I'm trying to change my body's background color to a random color every time a nav-bar link is clicked.
I got it so that a random color gets changed the first time a link is clicked - but when a different link is clicked, nothing changes.
Any tips?
HTML:
<div class="nav">
<ul id="show">
<li id="link1"><a href="#portfolio">Portfolio</li></a>
<li id="link2"><a href="#about">About</li></a>
<li id="link3"><a href="#email">Contact</li></a>
<hr/>
</ul>
</div>
JS:
$(function() {
var colors = ['red', 'blue', 'green', 'grey'];
var randColor = colors[Math.floor(Math.random()*colors.length)];
$('.nav a').click(function() {
$('body').css('background-color', randColor);
});
});
<li><a>…</a></li>and not<li><a>…</li></a>. And remove the<hr/>inside the<ul>.<ul>s and<ol>s can only have<li>s as child elements.