/    Sign up×
Community /Pin to ProfileBookmark

Background image question

Is it possible to set background image URL in CSS AND give it an ID. I need to give it an ID so I can change the src of the image later on in some javascript?

THanks.

J

to post a comment
HTML

5 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzSep 19.2006 — Sure an element with an ID can have a background image.

#mainNav {
background-image: url(mainnavbg.jpg);
background-color: #66f;
}
Copy linkTweet thisAlerts:
@john_nobleauthorSep 20.2006 — Sorry, I didn'r make myself clear

What I want to do is something like this

[U]css file[/U]

td.image{

background-image: url(mainnavbg.jpg);

//set an id to the background image so I can access it through javascript later on

}


[U]html[/U]

<table><tr><td class = "image"></td></tr></table>


[U]Javascript code[/U]

document.images.imageID.src = newImage.gif


Basically I want to be able to change the background image of the td.image class using javascript. Can this be done?
Copy linkTweet thisAlerts:
@KravvitzSep 21.2006 — Oh! You mean you want to modify a stylesheet. Why didn't you say so?

[url=http://www.quirksmode.org/dom/changess.html]Change style sheet[/url]
Copy linkTweet thisAlerts:
@gooncorpSep 21.2006 — [code=html]<table><tr><td class="image" id="yourId"></td></tr></table>[/code]
document.getElementById('yourId').style.backgroundImage = 'url("newBGPic.jpg")';
Copy linkTweet thisAlerts:
@john_nobleauthorSep 21.2006 — Thanks, thats what I needed.
×

Success!

Help @john_noble spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...