I'm attempting to to randomly load an image each time a user visits the site. I've followed a tutorial and a couple of previous threads on the issue and can't seem to get it working. The images are in the /images/ folder and the filenames are correctly entered into the array:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" />
<script type="text/javascript">
var images = ['OUT01ari.jpg' 'OUT02adobe.jpg' 'OUT03alife.jpg' 'OUT04chinup.jpg' 'OUT05datenightwinecologne.jpg' 'OUT06officechair.jpg' 'OUT07printer.jpg' 'OUT08whitewall.jpg' 'OUT09umbrella.jpg' 'OUT10converse.jpg' 'OUT11wardrobebar.jpg'];
$('#background').css({'background-image': 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'});
</script>
I've then entered the div in the body of the page, but to no avail:
<body>
<div ="#background"></div>
<div class="container">
</div>
</body>
Where am I going wrong?
To show background-image, a div must have some size/area, have-you tried this ?
?
should be
(Or was that just a typo?)
You must have comma separators between the array values when you define your array.
You should also have two separate script elements, one for including jquery and the other for your code.
The content of a script tag with a src-attribute should be ignored by most browsers.
W3C 4.3 Scripting HTML5 says:
And the same is true for earlier versions I believe.
Edit:
If you are working on the local file system , make sure to change the URL to jQuery to http:// instead of just //.
Also, make sure your script is executed when the #background element exists by calling in on document ready.
This example should work even locally: