Click here to Skip to main content
15,905,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please i want someone to help me fix my code, i am trying to show a hided menu with onclick function and as well show the same menu with onclick,but my problem is that when i click the menu will show but will not hide on the next click.

What I have tried:



<pre lang="HTML">
<div id = "toggle" onclick = "toggle()"><a href = "#">
<div class = "toggle" onclick = "toggle()"></div>
<div class = "toggle" onclick = "toggle()"></div>
<div class = "toggle" onclick = "toggle()"></div></a>
</div>

<div id = "tutorials-header-list">
<ul id = "menu">
<li id = "list-1001"><a href="#">Home</a></li>
<li id = "list-1002"><a href="#">About-us</a></li>
<li id = "list-1003"><a href="#">Library</a></li>
<li id = "list-1004"><a href="#">grammar-rule</a></li>
<li id = "list-1005"><a href="#">Online-Calculators</a></li>

</ul>

</div>


</pre>




#tutorials-header-list ul li{
float:left;
margin-right:40px;
display:inline-block;
position:relative;
bottom:0px;
height:50px;
width:100%;
text-align:left;
line-height:40px;
text-transform:uppercase;
box-shadow:0px 0px 0px 0px;
}

#menu{
display:none;
}



var el = document.getElementById('menu');
function toggle(){

if (el.style.display == 'none') {
el.style.display = 'block';

}else{
el.style.display = 'none';

}


}
Posted
Updated 3-Apr-17 21:06pm
Comments
Karthik_Mahalingam 3-Apr-17 11:33am    
can you host your code in jsfiddle and share the link.
ZurdoDev 3-Apr-17 15:08pm    
Just debug it and you'll fix it soon. Or use jquery's .toggle()

Set your var el = document.getElementById('menu'); inside the toggle() function.
 
Share this answer
 
Hi,

Give ID to each div and while calling the toggle() function pass the ID of that div using this.id.

Then access this value in toggle function and get the ID as below.




function toggle(x)
{
x.style.display = 'block';
}

This should work..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900