Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Using Javascript to change a cell background image

Reply
Thread Tools

Using Javascript to change a cell background image

 
 
Mark
Guest
Posts: n/a
 
      07-29-2006
IE creates an object of every ID'd HTML tag (so it appears), and each
object sets a property for any parameter I set in the tag. For example,
my HTML might be: <td id='cell1' background='myimg.jpg' width='30'> ...
</td> which IE will use to create a Javascript object called 'cell1'
with properties of background and width, like so:
cell1.background would have a value of 'myimg.jpg' and cell1.width would
have a value of 30.

I can change the width of the cell in my Javascript function using:
document.getElementById('cell1').width = 25;
and I can change the background using
document.getElementById('cell1').background = "yourimg.jpg";

But this doesn't work in all browsers because they (FireFox, for
example) doesn't create an object property (such as "width" and
"background") for each tag with an ID.

How do I get around this using non-IE browsers? I want my Javascript to
be able to change the background image of a table cell. It works in IE,
but not FireFox. I haven't tried others yet.

Mark
 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      07-29-2006
Mark said the following on 7/29/2006 10:19 AM:
> IE creates an object of every ID'd HTML tag (so it appears), and each
> object sets a property for any parameter I set in the tag. For example,
> my HTML might be: <td id='cell1' background='myimg.jpg' width='30'> ...
> </td> which IE will use to create a Javascript object called 'cell1'
> with properties of background and width, like so:
> cell1.background would have a value of 'myimg.jpg' and cell1.width would
> have a value of 30.


Welcome to the quirks and stupidity of IE.

> I can change the width of the cell in my Javascript function using:
> document.getElementById('cell1').width = 25;
> and I can change the background using
> document.getElementById('cell1').background = "yourimg.jpg";
>
> But this doesn't work in all browsers because they (FireFox, for
> example) doesn't create an object property (such as "width" and
> "background") for each tag with an ID.


No browser does it other than IE, that I know of.

> How do I get around this using non-IE browsers? I want my Javascript to
> be able to change the background image of a table cell. It works in IE,
> but not FireFox. I haven't tried others yet.


Use CSS and then change it's style properties:

<td id="cell1" style="background-image:myimg.jpg;width:30px">

document.getElementById('cell1').style.backgroundI mage = 'newImage.jpg';
document.getElementById('cell1').style.width = '22px';

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
 
 
 
Mark
Guest
Posts: n/a
 
      07-30-2006

> Use CSS and then change it's style properties:
>
> <td id="cell1" style="background-image:myimg.jpg;width:30px">
>
> document.getElementById('cell1').style.backgroundI mage = 'newImage.jpg';
> document.getElementById('cell1').style.width = '22px';
>

Thanks! This does the trick!

Mark
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change the background image in a table cell Jim Javascript 2 07-24-2008 01:19 AM
How to change a table cell's background color? HB ASP .Net 4 05-07-2006 08:52 PM
how to change the background image of Jtable Cell get_moumita@yahoo.co.in Java 1 05-02-2006 03:13 PM
javascript to change cell background colours on a timer. Peter Williams Javascript 3 06-01-2005 06:30 PM
How to set cell background based on cell value when datagrid is displayed RJ ASP .Net Datagrid Control 1 02-17-2005 09:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57