So I am making a simple webpage but i can't get my dropdown to work right. at this point is no longer even appearing and when it was it appeared in a horizontal line rather than vertical.
As is it doesn't display the dropdown. What am I missing? Do I have something extra that's unneeded?
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
window.onclick = function(e) {
if (!e.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
for (var d = 0; d < dropdowns.length; d++) {
var openDropdown = dropdowns[d];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.show {display:block;}
<nav>
<ul>
<li><a href='front.html'>Home</a></li>
<li><a href='intro.html'>Intro</a></li>
<li><a href='how.html'>How it Works</a></li>
<li><a href='best.html'>Using it</a></li>
<li class ="dropdown">
<a href="javascript:void(0)" class="dropbtn"
onclick="myFunction()">Examples</a>
<ul class="dropdown-content" id="myDropdown">
<div class="dropdown-content" id="myDropdown">
<li><a href='saga.html'>SAGA</a></li>
<li><a href='portfolio.html'>Portfolio Optimization</a></li>
<li><a href='circuits.html'>Evolved Circuits</a></li>
</div>
</ul>
</li>
<li><a href='about.html'>References</a></li>
</ul>
</nav>
` and a ``.`document.getElementById('myDropdown').classList` will affect only the first, so the `` will be left with `display: none`.
– Heretic Monkey Apr 18 '16 at 22:07