Showing & hiding div layers on button clicks

Div Layer show hide We can control a layer by connecting it to buttons. We can display or hide the layer by clicking the buttons. To create and manage a layer we will use div tags.

<input type=button name=b1 value='Click Me' onclick="any_function();";>

By using style property of the page we can control the layer.

We will use one on page style sheet for our div tag by defining its layout properties. We will keep this style definition inside our head tags. For more details on Style property visit our CSS section. We will use one JavaScript function to manage the property of the div tag identified by its id. Here is the code

<title>(Type a title for your page here)</title>
<style type="text/css">
div {
position: absolute;
left: 100px;
top: 200px;
background-color: #f1f1f1;
width: 180px;
padding: 10px;
color: black;
border: #0000cc 2px dashed;
display: none;

<script language="JavaScript">
function setVisibility(id, visibility) {
document.getElementById(id).style.display = visibility;

<body >

<input type=button name=type value='Show Layer' onclick="setVisibility('sub3', 'inline');";>
<input type=button name=type value='Hide Layer' onclick="setVisibility('sub3', 'none');";>

<div id="sub3">Message Box</div>


Using JQUERY show hide effects we can manage layers easily

Using image as button

In the above code we can use image as button to show and hide layers. This part is included in the above demo. Here is the code to add image in place of button.
<input type=image name=imb src='images/show-layer.jpg'  onclick="setVisibility('sub3', 'inline');";>

Using one single button to show or hide the message layer

The above code will display two buttons one of showing the layer and other for hiding the layer. We can combine the functionality of these two buttons to one and keep one button which can be toggled to show or hide layer. The caption displayed over the button can be changed by using value property of the button id. We will change this data also and display the appropriate label to give the message.
document.getElementById('bt1').value = 'Hide Layer';
By this we can change the text written over the button
We can manage the display of layer by this .

document.getElementById(id).style.display = 'none'; // To hide the layer
document.getElementById(id).style.display = 'inline'; // To display the layer
Here is the code of above demo

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<html> <head> <title>Demo of Show hide div layer onclick of buttons</title> <META NAME="DESCRIPTION" CONTENT="Demo for showing and hiding div layers through button clicks"> <META NAME="KEYWORDS" CONTENT="Demo Show layer, Demo hide layer, display div, hide div, button on click, button on click event, div property, div style set"> <style type="text/css"> div { position: absolute; left: 250px; top: 200px; background-color: #f1f1f1; width: 280px; padding: 10px; color: black; border: #0000cc 2px dashed; display: none; } </style> <script language="JavaScript"> function setVisibility(id) { if(document.getElementById('bt1').value=='Hide Layer'){ document.getElementById('bt1').value = 'Show Layer'; document.getElementById(id).style.display = 'none'; }else{ document.getElementById('bt1').value = 'Hide Layer'; document.getElementById(id).style.display = 'inline'; } } </script> </head> <body> <input type=button name=type id='bt1' value='Show Layer' onclick="setVisibility('sub3');";> <div id="sub3">Message Box</div> <br><br> </body> </html>

Using single button and more than one layer ( boxes )

To manage more than one layer we can use the same function and pass two ids of the message layer to the function. Inside the function in place of managing one layer we will mange two layers.
Inside the style declaration we will be using relative positioning as we will be displaying two layers. The code is here

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<html> <head> <title>Demo of Show hide div more than one layer onclick of a single button</title> <META NAME="DESCRIPTION" CONTENT="Demo for managing multiple layers from single button"> <META NAME="KEYWORDS" CONTENT="Demo more than one box, single button, show hide layer"> <style type="text/css"> div { position: relative; left: 250px; top: 200px; background-color: #f1f1f1; width: 280px; padding: 10px; color: black; border: #0000cc 2px dashed; display: none; } </style> <script language="JavaScript"> function setVisibility(id1,id2) { if(document.getElementById('bt1').value=='Hide Layer'){ document.getElementById('bt1').value = 'Show Layer'; document.getElementById(id1).style.display = 'none'; document.getElementById(id2).style.display = 'none'; }else{ document.getElementById('bt1').value = 'Hide Layer'; document.getElementById(id1).style.display = 'inline'; document.getElementById(id2).style.display = 'inline'; } } </script> </head> <body> <input type=button name=type id='bt1' value='Show Layer' onclick="setVisibility('sub3','sub4');";> <div id="sub3">Message Box 3</div> <br><br><br><br><br> <div id="sub4">Message Box 4</div> <br><br> </body> </html>

Two layers showing one at a time with button click

Instead of using common style for div layers , we will keep individual style property of the layers so it can be managed easily. Here we will keep one layer hidden and one visible at the time of page loading.

Then Inside the if condition we will change the status of the layers and make one visible and one hidden. So on each click one layer will be visible at a time.

<script language="JavaScript">
function setVisibility(id1,id2) {
if(document.getElementById('bt1').value=='Show Box 3'){
document.getElementById('bt1').value = 'Show Box 4';
document.getElementById(id1).style.display = 'inline';
document.getElementById(id2).style.display = 'none';
document.getElementById('bt1').value = 'Show Box 3';
document.getElementById(id1).style.display = 'none';
document.getElementById(id2).style.display = 'inline';

We will change the label or the value of the button accordingly. Changes in style property is here

<style type="text/css">
position: relative;
left: 250px;
top: 200px;
background-color: #f1f1f1;
width: 280px;
padding: 10px;
color: black;
border: #0000cc 2px dashed;
display: none;

position: relative;
left: 250px;
top: 200px;
background-color: #f1f1f1;
width: 280px;
padding: 10px;
color: black;
border: #0000cc 2px dashed;
//display: none;



this example is very simple to understand.Thank you


is there a way to make it so they can click the same link/button to hide or show it?


Yes, it is added now. Check the demo to see how single button is used to display and hide a layer


When you use more than one of these on a page, and have one open, you have to double click to open a second one. Is there a way to fix this so you only need to click once?
Mark Jacobs


Thank you so much..saved me a lot of time. A little tinkering and I have a show/hide one the company intranet home page...


This is awesome, exactly the information I needed to get a certain page done!


thanx it was helpfull to me


Thanks! This helped me a lot :)


Hi! This is very useful thanks! Is there a way to achieve the same with class rather than id? I really need your help! thanks
joey Pogi


This is great help... thank you superman...


Hi, Very nice! But I want to animate it what to do?


wow.... This is what i wanted to do...:) Thanks... It helped me a lot...


anyone have an idea for make more that one box. bt1 for one box bt2 for one box


It is added, you can use single button to manage two layers. Same way you can use two buttons to mange two layers.


great code ! it helps a lot ! I wonder how to make it works with a text-link and not with a button ?




Thanks alot....


Thank you very much for the tutorial!


i have 4 dive and by click on heading of any div show whole content of the div and hide other div and we again click on same heading then we get all the div


You can use JQuery for this and there is a FAQ script you can download from here.
Henrik Kryger Pallesen


You just saved my day. Thank you :-)
Ashesh Sharma


Thanks Its amusing

Ashesh Sharma


This is good but i need for merge the div after click a button(concept : there is two div for guest user. after an admin login into the site the second div have to hide and first div have to full fill the place.)can any one know how to do this


By using server side script this has to be done. Once admin login then based on session present or not we can decide which div layer to display. If admin not logged in then different layer can be shown.
Alan C Kmiecik


Change images be used for the buttons?


Really helpful and easy to understand thanks bro..


If in css property is overflow:auto then how to show and hide the div.


Is it possible to change the buttons for images?


Image can be used in place of buttons. One example by using image is added in first demo.
Andrew King


I wish to have two buttons, each opening a different box in different positions. How do you specify the two positions? Also what specifies Sub3, Sub4 etc.?
Ed Newms


Great code. I just need one more thing. I need the button click to open a page in a new window. So really a link more than a button but either way. Thanks


On button click the div appears...n i have a form in that div..while pressing the submit buton the div gets how do i keep that div remain there..n i want a back button as well..kindly help..thnx


Hi, I have a new question. How to make a button to show AND hide two boxes at once (on click)? Meaning: when page loads one box is visible and the other is hidden. On click the visible box turn on hidden and the hidden go visible.
Any suggestion or script is highly appreciated.


I did two buttons, each controlling a layer. Buttons should read "Reports"/"Close Reports" (on click) and "Reviews"/"Close Reviews" but both say "Close Reviews" when clicked. Help! (Your "Same way you can use two buttons to mange two layers." comment didn't help.) Thanks

Post your comments , suggestion , error , requirements etc here .

We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 All rights reserved worldwide Privacy Policy Disclaimer