views:

1274

answers:

6

Hi

I have a DIV background which I want to change randomly on page load from a selection of 3 PNGs.

I searched around and found this code:

$(document).ready(function() {
  var randomImages = ['img-restaurante-1','img-restaurante-2','img-restaurante-3'];
  var rndNum = Math.floor(Math.random() * randomImages.length);
  $("div.lacarta").css({ background: "url(/images/" + randomImages[rndNum] + ".png) no-repeat" });
});

But for some reason it’s not working, any ideas?

A: 

Open firebug and see if the browser tries to load the images. Does it? Are they 404s?

Ran
A: 

not sure how to do that I´m affraid. I do have firebug and work on mac. The page in question is= http://www.mesondedeus.com/new/lacarta.htm, and the div in question is #lacarta

Vickyboy
A: 

The problem is that you are searching for a div with the class lacarte in your code, but you need to search for a div with the id lacarte, like this: $('#lacarte') instead of $('div.lacarte')

(Note how jQuery uses CSS selectors)

Edit in response to your comment
You next problem is that the images do not exist, if you change te selector as i said, the code runs, but the images do not exist in http://www.mesondedeus.com/images/. Try it yourself, try loading http://www.mesondedeus.com/images/img-restaurante-3.jpg (as the script will try) and note how it doesn't load. You should make sure you have your images in the right directory.

Pim Jager
Thanks, have tried that but unfortunately still not working.
Vickyboy
A: 

var rndNum = Math.floor(Math.random() * randomImages.length);

This will never give you the last image in the list, as far as I can tell.

Math.random() gives you a number between 0 and 1 (non-inclusive). And to get 2 [index of the last image] you should get floor(1*2)=2

Regarding the images not loading - just use #lacarta, instead of .lacarta (ID vs classname)

Joel L
No You need Math.Floor, consider the simplest case of 1 array entry, the length is 1, but the biggest value for the index you want is 0, so you should floor the random value.
Pim Jager
so what should the code look like? var rndNum = ....
Vickyboy
i appreciate your patience. I´m fairly new to JQuery. Could you show me what the code should look like?
Vickyboy
Yup - just discovered I can't count :/
Joel L
Yup, un-downvoted you.
Pim Jager
A: 

Try making these changes:

$(document).ready(function() {
  var randomImages = ['img-restaurante-1','img-restaurante-2','img-restaurante-3'];
  var rndNum = Math.floor(Math.random() * randomImages.length);
  $("div#lacarta").css({ background: "url(../images/" + randomImages[rndNum] + ".png) no-repeat" });
});

I changed the div.lacarta to div#lacarta and I changed your background url from "url(/images/" to "url(../images/"

fudgey
A: 

finally got it to work with this code:

$(document).ready(function() { var randomImages = ['img-restaurante-1','img-restaurante-2','img-restaurante-3']; var rndNum = Math.floor(Math.random() * randomImages.length); $("div#lacarta").css({ background: "url(http://www.mesondedeus.com/new/images/" + randomImages[rndNum] + ".jpg) no-repeat" }); });

thanks for all your help

Vickyboy