I want to have a div use a randomly chosen background image.
I am going to be adding to the possible background images over time.

Right now I have bg1.jpg and bg2.jpg.  Eventually I might have dozens.

My first instinct is

var totalNum = 2;
var rndNum = Math.floor(Math.random() * totalNum);
$("div.target").css("background-image","url(/imagePath/bg" + rndNum  + ".jpg)");

Then, every time I add a new image, I change totalNum to be accurate.
Is there an easier way?  One that doesn't require me to keep changing the
script?

Glen

Reply via email to