Skip to content
Advertisement

Hide and Unhide div with the same button

I am using a code that unhides a hidden div.

HTML:

<div id="unhide"  style="display:none;">DUMMY TEXT</div>

<button id="expand" name="expand">Show The Div</button>

JS:

document.getElementById("expand").addEventListener("click", function() 
{
    document.getElementById('unhide').style.display = "block";
});

How can I make the same button hide the div after clicking it again? Is it possible to alter the code I am using now?

Advertisement

Answer

use toggle to simple hide and unhide div

$("#expand").click(function() {
    $("#unhide").toggle();
});
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement