I am looking for a way to toggle the visibility of items in a list when an input is clicked. The challenge I'm facing is that the page should load with the list hidden, but the code only functions correctly if the list starts out visible.
function toggleList() {
var itemList = document.getElementById('items');
if (itemList.style.display == "block") { // if item list is hidden, show it
itemList.style.display = "none";
} else { // if item list is shown, hide it
itemList.style.display = "block";
}
}
ul#items li {
display: block;
}
<nav id="menu">
<input type="image" src="_imagens/menuesboco.png" onclick="toggleList()" />
<ul id="items">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</nav>