faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

11 of 13 people (85%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can access the bgcolor color of a cell selected in Javascript

Apr 6th, 2008 19:13
ha mo, Jean-Bernard Valentaten, Colin Fraser, Sumegha,


> The short answer is that you cannot, but this is not the whole truth,
> and I do hope that someone will prove me wrong. 
> Forms and layers and images even text can be objects in a document, 
> but tables do not, I think, exist as objects in Javascript, which is 
> why I suspect they are difficult to handle in JavaScript. Difficult, 
> but not impossible.
> I suspect that you would need something like outerHTML to do this,
> making the table dynamic. To test this try something like : 
> <table><tr>
>   <td name="bgTest">
>     <img src="bg1.gif" onMouseover="bgTest.outerHTML="<td
> background="bg2.gif"></td>">
> </td>
> </tr></table>
> BTW, I think I got this from a book on JavaScript, so I would not be
> surprised to learn that this table will work in IE but not Netscape, 
> or vice versa.
> Good luck..
Considering the JavaScript/DOM specs (JavaScript 1.5 and higher only!!)
it should very well be possible to do this. Alas js1.5 is only available
in IE5.5+ and NS6+.
First off you need to give the td an id (something like <td
id="bgTest">).
in the img-tags' (you might also use an anchor-tag) onMouseOver (or
onMouseDown or onClick) you write something like this:
onClick="document.getElementById('bgTest').setAttribute('bgColor',
'#FFCC00');"
That should do the trick. Instead of #FFCC00 simply enter the color that
you wish the td to have.
Instead of using ids you might simply use the following code in the
onClick-Handler (or onMouseOver or onMouseDown) of the tag that sits
inside the td (I'd use an anchor-tag)
onClick="this.parentNode.setAttribute('bgColor', '#FFCC00');"
But keep in mind that this will only work with new browsers.
HTH,
Jean
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com