Apolo Radomer Apolo Radomer - 23 days ago 11
CSS Question

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?

Answer

use toggle to simple hide and unhide div

$("#expand").click(function() {
    $("#unhide").toggle();
});
Comments