Using 'Hide' and 'show' in a div

Asked

Viewed 3,002 times

0

I have a div that when I click on it it shows and hides an ul. With the code like this:

<ul class="homeMenuItens">
        <li>
            <div pagina='/produtos/incendio' class="homeMenuPai">Incendio</div>
            <ul id="incendio" class="homeMenuFilhos" style="display:none">
                <li  pagina='paginaSYS' onclick='window.location=\"/paginaSYS\"'>tituloMenuSYS</li>
            </ul>
        </li>
    </ul>

Jquery is like this:

$(".homeMenuPai").click(function() {
    $(".homeMenuFilhos").css('display','none');
    if($(this).parent().find(".homeMenuFilhos").css('display') == 'none'){
        $(this).parent().find(".homeMenuFilhos").show();
    }
    else{
        $(this).parent().find(".homeMenuFilhos").hide();
    }    

});

It’s working, as soon as I click on the Title Menu, for example, it shows the items below. The problem is that if I click Div more than once, it does not hide the items. It only hides when I click the other Divs.

The structure is like this div TITULO ul WITH ITEMS li LIST OF ITEMS

I have 4 equal Ivs

In the image below shows the 4 main menus, if I click on 2, the 1 closes, but if I click on 1 again to close, it does not close.

inserir a descrição da imagem aqui

  • It would be interesting to create a http://jsfidle.net to show exactly what is happening, so it will be easier to help you

  • Hi @Erloncharles will get complicated put in jsfidle because the code is full of reserved words, I put an image to facilitate understanding.

  • Just to understand you want something here to work like this? http://jqueryui.com/accordion/#Collapsible

  • 1

    @Erloncharles, Vieira helped me the way I needed, thanks. D

1 answer

1


The problem is that you are setting all the elements .homeMenuFilhos with display = none and then checking if it’s display == none, i.e., the if will always be true. Remove align:

$(".homeMenuPai").click(function() {
    //remova esta linha --> $(".homeMenuFilhos").css('display','none');
    if($(this).parent().find(".homeMenuFilhos").css('display') == 'none'){
        $(this).parent().find(".homeMenuFilhos").show();
    }
    else{
        $(this).parent().find(".homeMenuFilhos").hide();
    }    

});

Or if you want to hide all others and display only the current one do so:

$(".homeMenuPai").click(function() {

   if($(this).parent().find(".homeMenuFilhos").css('display') == 'none'){
        $(".homeMenuFilhos").css('display','none');
        $(this).parent().find(".homeMenuFilhos").show();
   }
   else{
        $(this).parent().find(".homeMenuFilhos").hide();
   }    

});
  • Hi @Vieira, ball show your tip. The problem, is that when I click on 2 I want to hide one of them and not leave two showing, understand?

  • Look at the edition I made

  • Whoa, 10-point guy, that’s exactly what I wanted! Hug

  • Blz, if it worked don’t forget to mark the answer as accepted.

  • I certainly didn’t make an appointment because I had to wait a few minutes. : D

  • To avoid messing with the display of the elements, I usually create a simple CSS class, after all other statements: .HID { display:none; }. So, to hide I add the class HID to the element, and to show, I only remove the class HID his.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.