I have a link on a page which causes a <div id="po" style="display:none;">some html</div>
to show or hide. The first time the hide/show link is clicked, the div is displayed. Any time after that, the link does nothing and I don’t see why. I did try conversely setting the <div style="display:block;">
and clicking the link caused the div to hide. Subsequently, the div would not show. I am not seeing why this does not work. I have the exact same thing elsewhere on the page and that div toggles.
function po() {
po = document.getElementById('po').style.display;
if (po == "none") {
document.getElementById('po').style.display = 'block';
document.getElementById('po_Menu').src = "images/menu_dash.jpg";
} else {
document.getElementById('po').style.display = 'none';
document.getElementById('po_Menu').src = "images/menu_plus.jpg";
}
}
<img id="po_Menu" src="https://via.placeholder.com/50"> <a href="po()">show/hide</a>
2
Answers
You’re clobbering the function here:
This assigns a new value to
po
, so future calls topo()
will fail because it’s no longer a function.Declare a new variable instead:
Besides the naming conflict/overwriting that you’re doing with
po
, you should avoid inline styles and instead use CSS classes, which reduce the amount of code you’ll need to write (see the omission of theif
statement below) and only use hyperlinks for navigation.See additional comments inline: