Hide submenu by clicking another

Asked

Viewed 246 times

3

I wish that when I clicked on a submenu item, what was active was hidden.

I found a similar script but I had a hard time adapting.

I am using the following script for the onclick submenu

Javascript para Dropdown

 var $j = jQuery.noConflict();
    $j(".abre-dropdown").click(function(){
        $j(this).children("ul").slideToggle();
    })
    $j("ul").click(function(p){
        p.stopPropagation();
    })

    var $ = jQuery.noConflict();
    var checkbox = $('#btn-menu'); 
    $('body').click(function() { 
    checkbox.prop("checked", false); 
    }); 
    $('#btn-menu, .menuMobile, .barra-menu').click(function(event) { 
    event.stopPropagation(); 
    });

HTML from the menu :

<nav class="menuMobile">
<ul>
    <li class="item-menu-escuro"><a href="http://<?php echo $url; ?>/home"><label style="float:left;" class="icon-home"></label>Página inicial</a></li>
    <li class="abre-dropdown"><a href="javascript:void(0)"><label style="float:left;" class="icon-location"></label>Nossa casa<label class="icon-down-dir"></label></a>
        <ul class="submenu-1">
            <li class="abre-dropdown"><a href="javascript:void(0)"><label style="float:left;" class="icon-home"></label>Sobre nós<label class="icon-down-dir"></label></a>
                <ul class="submenu-2">
                    <li><a href="http://<?php echo $url; ?>/nossa-casa#terreiro">A casa</a></li>
                    <li><a href="http://<?php echo $url; ?>/nossa-casa#mae-maria">Mãe Maria C.</a></li>
                    <li><a href="http://<?php echo $url; ?>/nossa-casa#ogans">Ogans</a></li>
                    <li><a href="http://<?php echo $url; ?>/nossa-casa#filhos">Filhos</a></li>
                </ul>
            </li>
<li><a href="http://<?php echo $url; ?>/agenda"><label style="float:left;" class="icon-calendar"></label>Agenda</a></li>
<li><a href="http://<?php echo $url; ?>/localizacao"><label style="float:left;" class="icon-location"></label>Localização</a></li>
<li><a href="http://<?php echo $url; ?>/contato"><label style="float:left;" class="icon-phone"></label>Contato</a></li>
        </ul>   
    </li>
</ul>
</nav>

1 answer

2


Thiago try to implement this code:

$j(".abre-dropdown a").click(function() {
  var item = $(this).parent();
  $(this).closest('ul').find('ul').not($(this).parent('li').children('ul')).slideUp();
  item.children('ul').slideToggle();
})

Example

Click on the items in bold.

$('li a').click(function() {
  var item = $(this).parent();
  $(this).closest('ul').find('ul').not($(this).parent('li').children('ul')).slideUp();
  item.children('ul').slideToggle();
})
.hide {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<ul>
  <li><a href="#">1. item</a></li>
  <li><a href="#"><b>2. item</b></a>
    <ul class="hide" >
      <li><a href="#"> 2.1. item</a> </li>
      <li><a href="#"> 2.2. item</a> </li>
      <li><a href="#"> <b>2.3. item</b></a>
        <ul class="hide" >
          <li><a href="#"> 2.3.1. item</a> </li>
          <li><a href="#"> 2.3.2. item</a> </li>
          <li><a href="#"> 2.3.3. item</a> </li>
        </ul>
      </li>
    </ul>
  </li>
  <li><a href="#">3. item</a></li>
  <li><a href="#">4. item</a></li>
  <li><a href="#"><b>5. item</b></a>
    <ul class="hide" >
      <li><a href="#"> <b>5.1. item</b></a>  
        <ul class="hide" >
          <li><a href="#"> 5.1.1. item</a> </li>
          <li><a href="#"> 5.1.2. item</a> </li>
          <li><a href="#"> 5.1.3. item</a> </li>
        </ul>
       </li>
      <li><a href="#"> <b>5.2. item</b></a>  
        <ul class="hide" >
          <li><a href="#"> 5.2.1. item</a> </li>
          <li><a href="#"> 5.2.2. item</a> </li>
          <li><a href="#"> 5.2.3. item</a> </li>
        </ul>
      </li>
      <li><a href="#"> 5.3. item</a> </li>
    </ul>
  </li>
</ul>

  • It didn’t work. I don’t know if I implemented it correctly. What I did was replace what Voce went through a similar part, and so it went on the same way. Then I added below what already existed, and when clicking the submenu went down and went up automatically..

  • @Thiagobarros. The right one was to replace even, would have as prepapar a Jsfiddle, with a similar operation to that of its site? It would be easier.

  • I’m trying to create Jsfiddle, but the script is not working so that the submenu can be clicked.. The code I’m trying to use in Jsfiddle is the same as the question.

  • 1

    @Thiagobarros, look at this example. It suits you?

  • 1

    I tried to add a second level submenu. However, when clicked, the whole menu hides again.. how could I add more levels? My javascript understands all subsmenus, but does not have the function to hide one when the other is active..

  • 1

    @Thiagobarros, I get it. I’m going to fix this and I warn you.

  • All right! Thank you!

  • 1

    @Thiagobarros, look at the edition. I imagine that now serves. rsrs

  • Perfect! Thanks for the help, it’s exactly what I imagined! Now I have to adapt to my menu! Congratulations ;)

Show 4 more comments

Browser other questions tagged

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