sidhewsar sidhewsar - 29 days ago 12
HTML Question

how to show or hide a div on button click using javascript

here the following are the codes

<style type="text/css">
#mydiv{ margin:0 auto;padding:0;width:250px;height:100px; border:1px solid threedshadow;display:none;}
</style>

<script type="text/javascript">
function show()
{
var div=document.getElementById('mydiv');
div.style.display == "none" ? "block" : "none";
}
</script>



<input type="button" onclick="show()" value="show"/>
<div id="mydiv">


this code does not working can anybody help me!!

Answer

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

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