I want the first li be opened by default and when I click on an "a" tag that is opened the ul following don't close

Forums jQueryI want the first li be opened by default and when I click on an "a" tag that is opened the ul following don't close
Staff asked 3 months ago
 Here my html :
    <ul id="navLeft">
      <li>
        <a class="nav_a">aaaa</a>
        <ul class="children">
          <li><a>1111</a></li>
          <li><a>1111</a></li>
          <li><a>1111</a></li>
        </ul>
      </li>
      <li>
        <a class="nav_a">bbbb</a>
        <ul class="children">
          <li><a>2222</a></li>
          <li><a>2222</a></li>
          <li><a>2222</a></li>
        </ul>
      </li>
      <li>
        <a class="nav_a">cccc</a>
        <ul class="children">
          <li><a>3333</a></li>
          <li><a>3333</a></li>
          <li><a>3333</a></li>
        </ul>
      </li>
    </ul>
    </body>
    
    CSS:
     .children{
      display: none;
    }


jquery:
     $(".nav_a").click(function () {
        $('ul.children').not($(this).next('ul.children')).removeClass("expanded").slideUp();
        $(this).next('ul.children').toggleClass("expanded").slideToggle(250);
    });

 

Answers (1)

Add Answer
Prince Dhameliya Marked As Accepted
Staff answered 3 months ago
 $(".nav_a").click(function () {
         if($(this).next('ul.children').hasClass("expanded"))
      return;    
  
  $('ul.children').not($(this).next('ul.children')).removeClass("expanded").slideUp();
        $(this).next('ul.children').toggleClass("expanded").slideToggle(250);

});

 

Subscribe

Select Categories