/    Sign up×
Community /Pin to ProfileBookmark

style.backgroundImage variable HELP

I am struggling with the following code. Can any one help me. How do I use a variable like “x” instead of “someimage.gif” in the following line? style.backgroundImage = url(someimage.gif)

I’m currently using a script to change the background color on any element with the class of “beadcolor1″…the following code is working!

function color1(x) {
//Populate the array with all the page tags
var allPageTags=document.getElementsByTagName(“*”);
//Cycle through the tags using a for loop
for (i=0; i<allPageTags.length; i++) {
//Pick out the tags with our class name
if (allPageTags[i].className==’beadcolor1′) {
//Manipulate this in whatever way you want
allPageTags[i].style.backgroundColor = x;
}
}
}

HOWEVER, instead of changing the background color, I want to change the background image. I have the following code but is not working. Any help?

function color1(x) {
//Populate the array with all the page tags
var allPageTags=document.getElementsByTagName(“*”);
//Cycle through the tags using a for loop
for (i=0; i<allPageTags.length; i++) {
//Pick out the tags with our class name
if (allPageTags[i].className==’beadcolor1′) {
//Manipulate this in whatever way you want
allPageTags[i].style.backgroundImage = x;
}
}
}

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@yamaharussFeb 01.2008 — What exactly is the value of x being passed? And are you getting any errors?

For instance, you can't call the function with

color1(myimage.jpg)

it must be

color1('myimage.jpg')

also, check to be sure your image directory is accurate.
Copy linkTweet thisAlerts:
@still-learningauthorFeb 01.2008 — on a form <select> <option> tag I inserted the following and it passed the variable.

<option onclick="color1(this.value)" value="url(images/gray.gif)">Gray</option>
Copy linkTweet thisAlerts:
@yamaharussFeb 01.2008 — on a form <select> <option> tag I inserted the following and it passed the variable.

<option onclick="color1(this.value)" value="url(images/gray.gif)">Gray</option>[/QUOTE]


Try...


<option onclick="color1(this.value)" value="url('images/gray.gif')">Gray</option>
×

Success!

Help @still-learning 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...