developing a dropdown menu feature

I'm struggling with a small issue related to my drop-down menu function. My goal is to hide the visibility of a menu tab after it has been clicked for the second time.

Below is the code snippet I've been working on:

HTML:-

<nav class="clearfix">
    <ul>
        <li class="navTab marginRight">
            <a><span class="iconFont">v</span></a>
            <ul>
                <li><a></a></li>
                <li><a></a></li>
            </ul>
        </li>
        <li class="navTab marginRight">
            <a><span class="iconFont">v</span></a>
            <ul>
                <li><a></a></li>
                <li><a></a></li>
            </ul>
        </li>
        <li class="navTab">
            <a><span class="iconFont">v</span></a>
            <ul>
                <li><a></a></li>
                <li><a></a></li>
            </ul>
        </li>
    </ul>
</nav>

CSS:-

.navTab ul.visible {
  visibility: visible;
}

.navTab ul {
  display: inline;
  visibility: hidden;
  padding: 0px;
  z-index: 200;
  position: absolute;
  left: 0;  
}

jQuery:-

$(document).ready(function(){
  var list = $('.navTab ul'); 

  $('li.navTab').click(function(){
    var thatIndex = $(this).index();
    list.not( ':eq(thatIndex)' ).removeClass('visible');
    list.eq(thatIndex).addClass('visible');
  });
});

Answer №1

Hello there, it seems like you are in need of this code snippet.

$(document).ready(function(){  
  $('li.navTab ul').hide();  
  $('li.navTab').click(function(){   
    $(this).find('ul').slideToggle();  
  });
});
<nav class="clearfix">
    <ul>
        <li class="navTab marginRight">
            <a><span class="iconFont">v</span></a>
            <ul>
                <li><a>sssss</a></li>
                <li><a>ads</a></li>
            </ul>
        </li>
        <li class="navTab marginRight">
            <a><span class="iconFont">v</span></a>
            <ul>
                <li><a>hhhh</a></li>
                <li><a>weq</a></li>
            </ul>
        </li>
        <li class="navTab">
            <a><span class="iconFont">v</span></a>
            <ul>
                <li><a>ac</a></li>
                <li><a>sadfs</a></li>
            </ul>
        </li>
    </ul>
</nav>

In case you haven't applied your custom styles yet, feel free to do so with this code.

For a demo of the code in action, click on the link below:

See Demo Code


Check out the updated code below:

$(document).ready(function(){  
  $('li.navTab ul').hide();  
  $('li.navTab').click(function(){
    debugger;
      if($('li.navTab').is(":visible")){
          $('li.navTab ul').hide();
          $(this).find('ul').slideToggle();  
      }    
  });
});

View Running Demo

Answer №2

Using the toggleClass() method might be a useful solution.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to implement Bootstrap in Coda2?

I am new to web design and I am trying to incorporate Bootstrap into my HTML code using Coda2. Since I don't have access to a server, I am referencing the directory address of CSS files on my hard drive instead of a web address. Currently, the beginni ...

Hover over images for cool effects

Check out the code snippet I created on this link The current functionality enlarges images by 20 percent and overlaps into the table on hover. How can I enlarge both the image and table dimensions simultaneously? Below is the HTML code: <table borde ...

Achieve full parent width with floated divs

My goal is to arrange 10 boxes horizontally on a webpage. Each box must have a minimum width of 150px and a maximum width of 299px. The challenge is to fit as many boxes as possible across the page without any gaps, ensuring that each box has an equal widt ...

Align two divs with text in the center

I am having trouble centering two divs horizontally and aligning them between each other. The issue arises when I add text of different sizes in each div. Here is an example: http://jsfiddle.net/DgEcs/1/ Why does the red div move down and how can this be ...

What are some methods for transferring the state variable's value from one component to another in React?

I have the following scenario in my code: there is a Form.js component that interacts with an API, stores the response in the walletAssets state variable, and now I want to create a separate Display.js component to present this data. How can I pass the v ...

Converting a C# two-dimensional array into JavaScript formatting

Is there a more streamlined method for converting a two-dimensional array in C# like [[1, 2, 3],[4, 5, 6]] into a string representation "[[1, 2, 3],[4, 5, 6]]" without manually iterating through each value and formatting it? I want to pass the array as an ...

Top techniques for dynamic loading of HTML and Javascript

I recently implemented a page that uses XHR requests to load a modal box containing a form. The form is comprised of HTML tags along with some JavaScript for validation and submission through another XHR request. Although the functionality works as intend ...

The end of the page is not displayed fully in Safari

I've been scratching my head trying to understand why this Safari browser issue is happening on this page: . It seems to be working perfectly fine on all other browsers that I've tested so far! The code includes some jquery and css to adjust the ...

The socket io server connection triggers repeatedly

Currently, I am working on developing a simple game using next.js and node.js. However, when I test the game, I notice that there are multiple "connected" logs being displayed. Even though I have only one client (with just a single tab open in Chrome), the ...

There seems to be confusion surrounding the $.isArray() function in jQuery

I'm currently trying to determine whether the p array is valid or not. I'm not sure if I'm on the right track or if I'm making a mistake. I am currently studying the jQuery.isArray method. I suspect that there may be an error in my code ...

Tips for crafting interactive Dropdown menus

Visit this link for more information Hello all, I am a beginner in HTML and Javascript and seeking guidance on how to create dynamic dropdown menus similar to the ones on the provided website. I have successfully implemented the source code, but my questi ...

Embedding the scrollbar within the div container

I'm having trouble placing my vertical scrollbar inside my div and adjusting its position. To streamline the code, I have extracted a portion below: .toprightcontrols { margin: 0 3% 0 0; display: flex; position: absolute; justif ...

Conceal or reveal input elements with a toggle function in jQuery by utilizing

When the user chooses an option from a dropdown list, I want to display or hide different input fields accordingly. I attempted to use toggle with boolean values, but it doesn't seem to be working as expected. I expect that if I send 'true' ...

Discover the power of using SVG sprites in Vue JS for dynamic, visually appealing CSS background

Utilizing an SVG sprite file in a Vue JS HTML template is successful with the following setup: <svg class="icon"> <use xlink:href="~@/assets/images/icons.svg#edit"></use> </svg> The contents of the icons.svg file typically resem ...

Failing to upload a file along with other form elements in Flask results in a 400 error

Encountering a 400 error when attempting to upload a file and send other form elements to Flask from HTML. Tried using AJAX, but it also gives me an error. Python: @app.route('/prod_diff_result', methods=['POST']) def prod_diff_result ...

How to Incorporate LessCSS into the Blueprint Framework?

Can LessCSS be integrated with Blueprint framework? What are the prerequisites for starting? ...

Achieving vertical center alignment for the label and btn-group in Bootstrap

Is it possible to align other elements with the Bootstrap btn-group? <div class="btn-toolbar text-center"> <div class="pull-left"> <span class="glyphicon glyphicon-envelope " style="font-size:1.5em;"></span> < ...

Adjusting the shadow on the inserted image

Currently, I am using fabric.js to manipulate images that are added to my canvas. The issue I am facing is with the shadow around the image not being even. The code I have tried so far is displayed below, but you can view what I am aiming for by clicking h ...

What strategies can be implemented to improve the total blocking time in Vue for optimal performance

I'm facing a challenge that I can't seem to resolve. My page has a high total blocking time (2+ sec). Despite trying to load every vue component asynchronously, the issue persists with 2+ sec TBT. I'm puzzled by what could be causing such a ...

Using PHP to pass variables to an external JavaScript file

I have come across the following code snippet: PHP <?php include("db.php"); ?> <html> <head> <title>Title</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/j ...