function showhide(id) {
obj = document.getElementById(id);
if (obj.style.display == 'none' || obj.style.display == null)
obj.style.display = 'block';
else
obj.style.display = 'none';
}
#stuff {
display: none;
}
<a href="#" onclick="showhide('stuff'); return false;">Click Me</a>
<div id="stuff">
secret stuff
</div>
This kinda works but require two clicks. It seems that it can’t detect that the state is null
.
Apparently if I change the condition to this it works:
if (obj.style.display == 'block')
obj.style.display = 'none';
else
obj.style.display = 'block';
My question now is, what is wrong with the first condition?