1
I have a problem that is simple but I do not use JS
a while back then I got a problem.
I’m having to create a menu similar to the Amazon, but the site is responsive and I did with lists. But it was not good so I’m doing with Divs (in case someone has better idea thank you) the problem is the following.
By clicking on a menu button it opens the div
for id
of the same, but when I click on another he should close the one that was open but I can’t close them.
I’m using the following code to stay as light as possible.
JS
$(function(){
$(".btn-toggle").click(function(e){
e.preventDefault();
el = $(this).data('element');
$(el).toggle();
});
});
html
<div id="menu_drop">
<ul class="menu_topo">
<li ><a href="#" ><button type="button" class="btn-toggle" data-element="#minhaDiv">Mídias e Embalagens</button></a></li>
<li><a href="#"><button type="button" class="btn-toggle" data-element="#minhaDiv1">Informática</button></a></li>
<li><a href="#">Eletrônicos</a></li>
<li><a href="#">Telefonia</a></li>
<li><a href="#">Papelaria e Escritório</a></li>
<li><a href="#">Cine e Foto</a></li>
<li><a href="#">Utilidades</a></li>
<li><a href="#">Esporte e Lazer</a></li>
<li><a href="#">Brinquedos</a></li>
</ul>
</div><!-- menu drop-->
<div id="minhaDiv" class="menu_lado">Conteudo</div>
<div id="minhaDiv1" class="menu_lado">Conteudo1</div>
It worked perfectly. I completely forgot to store the variable. There is only one small detail, when I click again on the menu it does not close to which it opened itself. Type: Click Menu 1 > Open Div 1 Click Menu 1 again > Close Div 1 Click Menu 1 > Open Div 1 Click Menu 2 > Close Div 1 and Open Div 2 TKS!
– Rodrigo B. Silva
@Rodrigob.Silva, I’ve corrected this detail. Run the code again. I switched
toggle()
forshow()
unintentionally– Caio Felipe Pereira
For some reason it’s not working when I implement it. I even copied the code into another HTML and it doesn’t work oo'
– Rodrigo B. Silva
This is generating some error?
– Caio Felipe Pereira
simply does not open or close, as if there was no JS.
– Rodrigo B. Silva
You entered the jquery, right?
– Caio Felipe Pereira
Yes! heheheheh The code that Iazyfox posted works until ( the problem is that it doesn’t close again by clicking on it), now its only worked with the first code posted.
– Rodrigo B. Silva
Curious, I tested it here and it’s working perfectly...
– Caio Felipe Pereira