I am trying to get jQuery to generate a random background on my site body based off a number. So I’m trying to get it to generate either main1.jpg or main2.jpg and throw it into the body as a background.
For some reason it is only generating main2.jpg. Here is my code:
$(document).ready(function(){
$("body").css({'background' : 'url(images/main1.jpg)'}).hide();
$("body").css({'background' : 'url(images/main2.jpg)'}).hide();
var randomNum = Math.floor(Math.random()*2); /* Pick random number */
$("body").css({'background' : 'url(images/main:eq(' + randomNum + ').jpg)'}).show(); /* Select div with random number */
});
Thanks,
Wade
It is very confusing to see what you are trying to do. Right now you are hiding the body twice, then incorrectly adding a css rule, then showing the body. If you just want to set a random background, do this:
If you are wanting to add two
divsand show one randomly, do this: