Introduction
Recently I was asked how to create a simple JavaScript hidden menu. Such a menu could be required for any number of reasons and is a common task in the world of web development. There are many ways in which this task could be performed and the right solution for your needs may not be the same. The menu I will share with you today is the simplest way.
The question posed to me was this “What is the simplest way to show and hide a menu in the JavaScript onclick event that won’t move my content?”
Upon pondering that question I came up with what I am going to show you below.
First we need to write some HTML. There is nothing in the HTML that is complicated in any way, just a button and an unordered list. Then we will write some basic CSS classes that will set the position of the elements, the button, and the list.
We will also set the display property in the CSS. We will do this in two separate classes one that sets the display to none and one that sets the display to block. After we have written the CSS we will move on the JavaScript. We will use JavaScript to overwrite the class associated with the list.
- <button id="btnMenu">Click me!</button>
- <ul id="menu">
- <li><a href="http://www.griffithswebdesign.com">Item 1</a></li>
- <li><a href="http://www.c-sharpcorner.com">Item 2</a></li>
- <li><a href="http://www.google.com">Item 3</a></li>
- </ul>
First we will set the position of the button and set the height & width, like so,
- button
- {
- width: 100 px;
- height: 30 px;
- position: absolute;
- top: 10 px;
- left: 1 px;
- }
Now we need to set the position of the list and we also want to set it’sz-index property so that it will sit over the top of anything underneath rather than disturbing the layout of the page.
- #menu
- {
- position: absolute;
- top: 10 px;
- left: 101 px;
- z - index: 50;
- }
Our next step in writing the CSS is the most important step of them all, which is to create the classes the switch the display property from none to block and back again.
- .menuOff
- {
- display: none;
- }
- .menuOn
- {
- display: block;
- }
Before we proceed to the JavaScript go back to your HTML and modify your opening ulelement. The additions are highlighted in yellow.
- <ul id="menu" class="menuOff">
If the current class is menuOff then we want to overwrite it with the class menuOn and vice versa.
- function buttonclick()
- {
- var menuList = document.getElementById("menu");
- if (menuList.className == "menuOff")
- {
- menuList.className = "menuOn";
- } else
- {
- menuList.className = "menuOff";
- }
- }
There is one more little thing we need to do before we have finished, we need to call our function. We will do this by modifying the button opening element in your Html. The additions are highlighted in yellow.
- <button id="btnMenu" onclick="buttonclick();">


I hope you found this article helpful. If you have any comments, questions, or any other feedback I would love to hear them; please mention in the comments section.
Read more articles on JavaScript

Shaili DashoraPosted Mar 5, 2016, 2:04 PM
Nice one
Gowtham KPosted Feb 28, 2016, 12:32 PM
Good One, Thanks for sharing
Humayun Kabir MamunPosted Feb 27, 2016, 10:42 PM
Nice...
Sibeesh VenuPosted Feb 27, 2016, 9:58 AM
Nice Share
Vignesh ManiPosted Feb 27, 2016, 9:08 AM
Good
Asfend YarPosted Feb 27, 2016, 3:44 AM
nice start
Pankaj Kumar ChoudharyPosted Feb 27, 2016, 1:38 AM
Nice Start Michael.............
Dwi HyugaPosted Feb 26, 2016, 10:46 PM
thank
Mohammed IbrahimPosted Feb 26, 2016, 1:17 PM
nice
Amit Kumar SinghPosted Feb 26, 2016, 1:13 PM
Nice One Sir .... :)