Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparative structures with CSS

Status
Not open for further replies.

james6848

Technical User
May 10, 2004
23
GB
Hi,
I can't seem to get this sort of structure to work:
Code:
if (myobject.style.backgroundImage  == "url(../Images/bluebackground1light.jpg)") {
myobject.style.backgroundImage  = "url(../Images/bluebackground1dark.jpg)";
}

I know I've created the object OK, and I know all the URLs are OK. I'm just wondering if it is actually possible to do a CSS comparison within the if statement.
 
change:
if (myobject.style.backgroundImage == "url(../Images/bluebackground1light.jpg)")

to:

if (myobject.style.backgroundImage.indexOf("bluebackground1light.jpg")!=-1)
{
myobject.style.backgroundImage = "url(../Images/bluebackground1dark.jpg)";
}

u cannot compare the images as such, but you can take the path and compare it...

Known is handfull, Unknown is worldfull
 
Try an alert to see what the string is

Code:
alert(myobject.style.backgroundImage)

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

zen.gif

 
vbkris - you posted faster then I did - I like your suggestion better...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

zen.gif

 
That looks pretty cool, thanks. If you have a moment, could you give me a bit of info on the indexOf() method?
 
myString.indexOf("stringToFind")

returns the character number where "stringToFind" is found in "myString" if the first letter is where it starts it returns 0 because strings are counted from 0 on. If the value is not found the result is -1


myString = "Hello There"
myString.indexOf("ll") = 2
myString.indexOf("He") = 0
myString.indexOf("abc") = -1

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

zen.gif

 
OK, thanks - I think I understand what's going on now.

I didn't appreciate how JavaScript treats all the CSS properties as strings - interesting. I guess that's why they always have to be within quotation marks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top