Forum Moderators: open

Message Too Old, No Replies

Chaning background image of an element onload (NOT BODY)

         

Ginger monkey

3:52 pm on Nov 8, 2007 (gmt 0)

10+ Year Member



Hi,

Grateful for anyone's help, I'm a novice at js and have copied this script from a website.

I've changed the element targeted from body to target the div #hero and I'm able to set the bg color with the same script but not the bg image. I'm also able to change the bg image of the body with it but not this div. The paths are correct (relative to javascript and html file) so I'm at a complete loss.

Here is the relevant code:

function runBGSlideShow(){
if (!document.getElementById) return false;
var slideshow = document.getElementById("hero");
slideshow.style.background = ../images/home_content_hero3.jpg';
}

I've seen loads of examples saying this is how you do it so I must be doing something wrong.

TIA

Dabrowski

4:11 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this a direct copy? There's a quote mark missing:

slideshow.style.background = '../images/home_content_hero3.jpg';

Ginger monkey

4:19 pm on Nov 8, 2007 (gmt 0)

10+ Year Member



it doesn't work with the quote mark there either. i must have deleted it while posting it.

this works:

function runBGSlideShow(){
if (!document.getElementById) return false;
var slideshow = document.getElementById("hero");
slideshow.style.background = 'red';
}

but this doesn't;

function runBGSlideShow(){
if (!document.getElementById) return false;
var slideshow = document.getElementById("hero");
slideshow.style.background = '../images/home_content_hero3.jpg';
}

So logically it could be the image paths but these are correct unless they're not relative to the javascript doc...? I've just tried putting the image in the same location as the script and that won't work either. Completely stuck now..

mehh

5:08 pm on Nov 8, 2007 (gmt 0)

10+ Year Member



try:
function runBGSlideShow(){
if (!document.getElementById) return false;
var slideshow = document.getElementById("hero");
slideshow.style.background = 'url(../images/home_content_hero3.jpg)';
}
or
function runBGSlideShow(){
if (!document.getElementById) return false;
var slideshow = document.getElementById("hero");
slideshow.style.backgroundImage = '../images/home_content_hero3.jpg';
}

Ginger monkey

6:04 pm on Nov 8, 2007 (gmt 0)

10+ Year Member



I've solved it now! It needed url() around the image paths. When it's used on document.body it didn't need it.

Thanks for your help anyway.

:)

Dabrowski

6:12 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aah yes of course. Mehh that was going to be my next suggestion!

Glad you got it sorted.