Forum Moderators: open

Message Too Old, No Replies

checking a <TD> background image

         

JSlice

4:11 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



Here's what I thought would be a simple question, but it's been giving me lots of trouble.

I want to do a check to see what the background image is for a certain table cell. I've tried all sorts of things, but it never works. My current statement that errors is this:

"if(document.x4y5.style.background == 'grass.gif')"

x4y5 is the id of the table cell. I need that to be a variable eventually, but it would be nice to have at least one work for now.

Can anyone suggest the best way to do this using only javascript?

Thanks,
Jslice

BlobFisk

4:12 pm on Jul 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, JSlice!

Try:

document.getElementById('x4y5').style.background == 'grass.gif')

JSlice

5:35 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



That bit of code doesn't give me an error, but it doesn't work either. the stuff in the if then statement doesn't execute, even though it should. (the picture is grass.gif)

Any other suggestons? I'm still messing with it, I'll post some more code when I figure out which part needs work...

thanks,
Jslice

Followup Info: It works if I set it to say!= 'grass.gif' so it must think it's something else. Is there anything about this line that makes it wrong?

<TD background="grass.gif" id=x4y5>

also, with a little further testing, it says that the background is undefined. Do I need to pass something into the function to make this work? I thought things in the document were global info...

garann

6:06 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



JSlice,

It looks like your script is looking for a style attribute, not the background attribute, which is what your TD tag has. Does it work if you change the TD tag to


<TD style="background:'grass.gif';" id="x4y5">

?

<added> Yeah, I'm slow. Glad you got it figured out. :) </added>

[edited by: garann at 6:16 pm (utc) on July 16, 2003]

JSlice

6:07 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



Ok, so I figured out that you have to take out the ".style" portion from that statement in order for it to work. Which doesn't make any sense to me, but oh well, I guess.

- Jslice

JSlice

6:08 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



heh, it looks like your helpful answer was just a few seconds late. thanks though :)

- Jslice

tedster

6:13 pm on Jul 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not 100% sure on this, but wouldn't the style atribute be backgroundImage in javascript, not simply background -- and even then, the value would be "(url)grass.gif" not simply "grass.gif".

garann

6:22 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



tedster, I'm pretty sure you can do either. It just depends on how you define it initally.

background: 'grass.gif'; == background: url(grass.gif); ==
background-image: 'grass.gif'; == background-image: url(grass.gif);

.. Right?

Whatever you put in your JavaScript just has to reflect the sytax you used in the stylesheet.