Skip to content Skip to sidebar Skip to footer

How To Show Or Hide A Div On Button Click Using Javascript

here the following are the codes

Solution 1:

Close, but you're not assigning the display value to the display property. Try this:

div.style.display = (div.style.display == "none") ? "block" : "none";

Solution 2:

You should use:

div.style.display = div.style.display == "none" ? "block" : "none";

Solution 3:

HTML

<input type="button" onclick="show()" value="Click Me"id="myBtn"/> 
<div id="mydiv">This is Div</div>

CSS:

#mydiv{ 
 margin:0 
 auto;padding:0;
 width:250px;
 height:100px;
 border:1px solid threedshadow;
 display:none;}

Script:

var showBtn = document.getElementById('myBtn');

showBtn.onclick = function() {
    var showme = document.getElementById('mydiv');
    if (showme.style.display !== 'none') {
        showme.style.display = 'none';
    }
    else {
        showme.style.display = 'block';
    } };

Post a Comment for "How To Show Or Hide A Div On Button Click Using Javascript"