Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ul li toggle menu

I have my HTML code here :

<ul class="main-block">

 <li class="firstLevel">
   <a href="#category">EXAMPLE CATEGORY 1</a>
     <ul class="dijete">
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 11.</a>
       </li>
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 1.1</a>
       </li>
     </ul>
 </li>

 <li class="firstLevel">
   <a href="#category">EXAMPLE CATEGORY 2</a>
     <ul class="dijete">
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 2.1</a>
       </li>
       <li class="child">
          <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 2.2</a>
       </li>
     </ul>
 </li>

</ul>

My jQuery code for toggle() is here:

<script type="text/javascript">
    var $j = jQuery.noConflict();
    $j(function() {
        $j('li.firstLevel').click(function(){
            if($j('ul.dijete').hasClass('active')){
                $j(this).find('ul.dijete').removeClass('active');
            }else{
                $j(this).find('ul.dijete').addClass('active');
            }
        });
    });
</script>

But when I am on one category EXAMPLE 1 (active or clicked already) and chose to click on another EXAMPLE 2 - the first one does not close, so as the other second that I clicked doesn not open.

Why is that hide-show doesn't work?

Why I can not show second sub-menu and hide first one while I am currently on first (not working in both ways)?

like image 591
Ho Thanh Cyberdelia Nhan Avatar asked Mar 08 '16 22:03

Ho Thanh Cyberdelia Nhan


2 Answers

Use this:

var $j = jQuery.noConflict();
$j(function() {
    $j('li.firstLevel').click(function(){
        $j(this).children('ul.dijete').toggleClass('active');
    });
});
like image 189
peppinoibra Avatar answered Sep 30 '22 06:09

peppinoibra


Working fiddle

You could use toggleClass() function instead and before adding active class to the clicked item you should remove it from other items with class dijete using :

$('ul.dijete').removeClass('active');

Hope this helps.


Working snippet

$('li.firstLevel').click(function(e){
  e.preventDefault();

  $('ul.dijete').removeClass('active');
  $(this).find('ul.dijete').toggleClass('active');
});
.active{
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="main-block">

  <li class="firstLevel">
    <a href="#category">EXAMPLE CATEGORY 1</a>
    <ul class="dijete">
      <li class="child">
        <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 11.</a>
      </li>
      <li class="child">
        <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 1.1</a>
      </li>
    </ul>
  </li>

  <li class="firstLevel">
    <a href="#category">EXAMPLE CATEGORY 2</a>
    <ul class="dijete">
      <li class="child">
        <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 2.1</a>
      </li>
      <li class="child">
        <a href="some-sub-category.html">EXAMPLE SUB-CATEGORY 2.2</a>
      </li>
    </ul>
  </li>

</ul>
like image 21
Zakaria Acharki Avatar answered Sep 30 '22 05:09

Zakaria Acharki