Change background to random colors on click - Javascript/jQuery -
i'm trying change body's background color random color every time nav-bar link clicked.
i got random color gets changed first time link clicked - when different link 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); }); });
because generate 1 random number , keep using it, move inside of click method.
Comments
Post a Comment