I am trying to figure out what was wrong with this code:
function toggle_visibility() {
var dropDownNav = document.getElementsByClassName("dropDownNav");
if (dropDownNav.style.display == 'block') e.style.display = 'none';
else dropDownNav.style.display = 'block';
}
The HTML goes like this:
<a class="dropDownNavButton" href="#" onclick=
"toggle_visibility(dropDownNav)"><img alt="menu icon" src=
"img/navIcon.png"></a>
<ul class="dropDownNav">
<li class="liWorks">
<a href="works.html">Works</a>
</li>
<li class="liAbout">
<a href="#">About</a>
</li>
<li class="liContact">
<a href="contact.html">Contact</a>
</li>
<li class="liBlog">
<a href="#">Blog</a>
</li>
</ul>
And yet Chrome tells me: Uncaught ReferenceError: dropDownNav is not defined onclick
Could anybody show me the way?
Thanks alot for all the quick answers! First time using stackoverflow, definitely not the last. It finally worked, I added the index[0] for the array, and removed dropDownNav on the onClick function. The only problem is, I would like the dropDownNav display to go back to none when I click on the a second time. Any clue?
dropDownNav[0].style.displaydropDownNavtotoggle_visibility?var dropDownNav = document.getElementsByClassName("dropDownNav");outside the function.dropDownNavas an argument in the onclick attribute, which would be undefined unless it was outside the function. But I can see he is not using it inside the function.