473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Highlighting Table Cells with JS - Firefox vs. IE

Hi,

I am a JS newbie. Hopefully the answer to my question is not trivial.

I have written a simple webpage that presents a table. A JS function
allows the viewer to click on a cell and each cell with the same
content has the background color changed to lime green. Each cell that
was previously highlighted has the background color changed back to
white. This works great with Firefox but does not work correctly with
IE. In IE, none of the previously highlighted cells has the background
color changed back to white. How can I modify the syntax so that it
will work in both browsers? Below is the JS function. A simple
webpage that will demonstrate the problem is at

http://uia.dyndns.org/test.html

Thanks for any help. Thanks also to Martin Honnen! His posts showed
me how this could be done.

Lance

<script type="text/javascript">
// Script for traversing all of the cells in all of the tables in the
document
function highlight_cell (element) {
var allTables = document.getElementsByTagName('TABLE');
var save_cell_contents=element.innerHTML
for (var i=0; i<allTables.length; i++){
table=allTables[i]
for (var row_index = 0; row_index < table.rows.length;
row_index++) {
for (var c=0; c < table.rows[row_index].cells.length; c++){

if(table.rows[row_index].cells[c].innerHTML==save_cell_contents){
table.rows[row_index].cells[c].bgColor='lime';
}
else
if(table.rows[row_index].cells[c].bgColor=='lime')
table.rows[row_index].cells[c].bgColor='white';
}
}
}
}
</script>

Jul 23 '05 #1
14 3384
ASM
lance wrote:
webpage that will demonstrate the problem is at

http://uia.dyndns.org/test.html
Try to correct all your :
<td bgcolor=" white"
by
<td bgcolor="white"

And perhaps prefer css than html colorization

<style type="text/css">
td { background-color: white; }
</style>
<script type="text/javascript">
// Script for traversing all of the cells in all of the tables in the
document
function highlight_cell (element) {
var allTables = document.getElementsByTagName('TABLE');
var save_cell_contents=element.innerHTML
for (var i=0; i<allTables.length; i++){
table=allTables[i]
for (var row_index = 0; row_index < table.rows.length;
row_index++) {
for (var j=0; j < table.rows[row_index].cells.length; j++){
// and using css ?
var c = table.rows[row_index].cells[j];
c.style.background = c.innerHTML==save_cell_contents? 'lime':
c.style.background=='lime'? 'white':'';
}
}
}
}
</script>


tested with succes on FF and IE5.1 IE5.2 (new) Mac
here :
http://perso.wanadoo.fr/stephane.mmo...ight_cells.htm
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #2
On Monday 04 July 2005 21:07, lance(ra************@comcast.net) wrote in
message <11**********************@g47g2000cwa.googlegroups .com>
Hi,

I am a JS newbie. Hopefully the answer to my question is not trivial.

I have written a simple webpage that presents a table. A JS function
allows the viewer to click on a cell and each cell with the same
content has the background color changed to lime green. Each cell that
was previously highlighted has the background color changed back to
white. This works great with Firefox but does not work correctly with
IE. In IE, none of the previously highlighted cells has the background
color changed back to white.


I'm doing something similar at work where I'm creating a What's On calendar.
It's in HTML4.01-Strict, if that makes a difference.

Obviously, the 'blahblahblah' is replaced with whatever method you use to
select each element.
eg: document.getElementByID('myID').style.backgroundCo lor="#FF0000"

To highlight I use:
blahblahblah.style.backgroundColor="#FF0000'

and to remove the higlighting I use:
blahblahblah.style.backgroundColor=""

I think "bgColor" should be "style.backgroundColor" but I could be wrong.
I'm no javascript expert, I just hit my head repeatedly against the
keyboard until it works.
Jul 23 '05 #3
ASM
PDannyD wrote:

I think "bgColor" should be "style.backgroundColor" but I could be wrong.
I'm no javascript expert, I just hit my head repeatedly against the
keyboard until it works.


bgColor is not an error -> that's work on FF
but preferably used only with document

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #4
ASM wrote:
PDannyD wrote:

I think "bgColor" should be "style.backgroundColor" but I could be wrong.
I'm no javascript expert, I just hit my head repeatedly against the
keyboard until it works.

bgColor is not an error -> that's work on FF
but preferably used only with document


FF may not report it as an error, but the property is indeed
backgroundColor.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #5
ASM
Randy Webb wrote:
ASM wrote:
bgColor is not an error -> that's work on FF
but preferably used only with document

FF may not report it as an error, but the property is indeed
backgroundColor.


FF/Tidy tells me :
Warning: <td> attribute "bgcolor" has invalid value " white"
and not :
Warning: <td> attribute "bgcolor" is invalid attribute
but on same time he advices that it is deprecated :
http://www.w3.org/TR/html4/conform.html#deprecated

As I see any doctype in lance's page
I can considere that page is in HTML 3.2 and by the fact : correct :-)

Anyway I've given a link to this page corrected with css

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #6
ASM
ASM wrote:

Anyway I've given a link to this page corrected with css


link broken :-(

http://perso.wanadoo.fr/stephane.mor...ight_cells.htm

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #7
Thanks gentlemen.

The simple fix that makes the script work on IE is changing the syntax
of bgColor to style.backgroundColor, i.e.

table.rows[row_index].cells[c].bgColor='lime'

to

table.rows[row_index].cells[c].style.backgroundColor='lime'
Stephane, I like the effect of changing the cell's color with 'hover'.
However, the effect does not seem to work with IE. I can't get the
color change effect when the mouse hovers over a cell to work in my
script on a cell that has already been clicked on once even though I
have copied what I think is the appropriate syntax from your script.
Here is a link to the new and improved script

http://uia.dyndns.org/test.html

Thanks again,

Lance

Jul 23 '05 #8
lance wrote:
Thanks gentlemen.

The simple fix that makes the script work on IE is changing the syntax
of bgColor to style.backgroundColor, i.e.

table.rows[row_index].cells[c].bgColor='lime'

to

table.rows[row_index].cells[c].style.backgroundColor='lime'
Stephane, I like the effect of changing the cell's color with 'hover'.
However, the effect does not seem to work with IE. I can't get the
color change effect when the mouse hovers over a cell to work in my
script on a cell that has already been clicked on once even though I
have copied what I think is the appropriate syntax from your script.
Here is a link to the new and improved script

http://uia.dyndns.org/test.html

Thanks again,


Try this one:
<script type="text/javascript">
function highlight_cell (element) {
var allTables = document.getElementsByTagName('TABLE');
var save_cell_contents = element.innerHTML;
var c, cells, j, i = allTables.length;

while( i--) {
cells = allTables[i].getElementsByTagName('TD');
j = cells.length;

while ( j-- ) {
c = cells[j];
if ( save_cell_contents == c.innerHTML ) {
c.style.backgroundColor = 'lime';
} else {
c.style.backgroundColor = 'white';
}
}
}
}

function doCell(e) {
e = e || window.event;
var tgt = e.target || e.srcElement;
while ( tgt.parentNode && 'TD' != tgt.nodeName ) {
tgt = tgt.parentNode;
}
if ( 'TD' == tgt.nodeName ) {
highlight_cell(tgt);
}
}

window.onload = document.body.onclick = doCell;
</script>

<table>
<tr><td>A cell</td><td>A cell</td><td>A cell</td></tr>
<tr><td>B cell</td><td>A cell</td><td>B cell</td></tr>
<tr><td>A cell</td><td>C cell</td><td>A cell</td></tr>
<tr><td>A cell</td><td>B cell</td><td>A cell</td></tr>
</table>
<hr>
<table>
<tr><td>A cell</td><td>A cell</td><td>A cell</td></tr>
<tr><td>B cell</td><td>A cell</td><td>B cell</td></tr>
<tr><td>A cell</td><td>A cell</td><td>A cell</td></tr>
<tr><td>A cell</td><td>B cell</td><td>C cell</td></tr>
</table>
--
Rob
Jul 23 '05 #9
ASM
lance wrote:

Stephane, I like the effect of changing the cell's color with 'hover'.
However, the effect does not seem to work with IE.
Yes I know
I am already surprised that onclick works in td with IE ! So ...

In IE 'hover' works only with links

You can try to ask Bill he fixes that pb :-)

I can't get the
color change effect when the mouse hovers over a cell to work in my
script on a cell that has already been clicked on once
Yes that's right
didn't go further ...

You can delete this rule from the css if it's not convenient.
Here is a link to the new and improved script

http://uia.dyndns.org/test.html

Thanks again,


For IE (an others ?) there is an hover altenative here :
http://perso.wanadoo.fr/stephane.mor...ght_cells2.htm
but very uggly because onmouseover and onmousout on each td :-(

Don't understand ... don't work with iCab :-(
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #10
ASM
RobG wrote:
Try this one:


on Mac, IE 5.2 and FF both tell me :
document.body is not an object :-(

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #11
ASM wrote:
RobG wrote:
Try this one:

on Mac, IE 5.2 and FF both tell me :
document.body is not an object :-(


Works for me with Firefox 1.0.4 and Safari 1.0.3 (OS X 10.2.8) and
even IE 5.2.

Probably a better alternative is to attach the event in the body tag:

<body onclick="doCell(event);">

or add it from the script using the modified:

document.body.onclick = doCell;

Note missing '(event)' as that is passed anyway when attaching
events this way in Firefox, Safari, et al.

--
Rob
Jul 23 '05 #12
ASM
RobG wrote:
ASM wrote:
on Mac, IE 5.2 and FF both tell me :
document.body is not an object :-(

Works for me with Firefox 1.0.4 and Safari 1.0.3 (OS X 10.2.8) and
even IE 5.2.


could you put in line your page ?
probably I made a mistake doing mine ?

I have really to understand your doCell()
Probably a better alternative is to attach the event in the body tag:

<body onclick="doCell(event);">

or add it from the script using the modified:

document.body.onclick = doCell;

Note missing '(event)' as that is passed anyway when attaching
events this way in Firefox, Safari, et al.


tryed all that (even in table) without succes :-(

I'm there :
special IE (and others ?) :
http://perso.wanadoo.fr/stephane.mor...t_cells_IE.htm
special normal (gecko ?) :
http://perso.wanadoo.fr/stephane.mor..._cells_CSS.htm

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #13
I have learned a bit more --

Using the bgColor attribute works fine in both IE and FF. The
difference between the two browsers is that when this attribute is
queried, the color reported is in different formats, e.g. 'orange' in
FF, and 'ffa500' in IE. Setting the bgColor attribute and then
querying the style.backgroundColor attribute does not work. This may
be obvious to the enlightened.

It appears the using style.backgroundColor works almost equivalently as
long as you are consistent. The difference is that colors are reported
in the text format, e.g. 'orange', in both FF and IE.

Here are two scripts that create a small table with some default
background colors. If you click on a cell, all cells with the same
cell content turn green, all other cells get turned to white if they
are green or orange. The first script used the bgColor attribute
consistently and the second script uses the style.backgroundColor
attribute consistently. Turn the alerts on or off to understand the
differences. Hopefully, this is not obvious to everyone.

http://uia.dyndns.org/test2.html
http://uia.dyndns.org/test3.html

Thanks for the replies!

Jul 23 '05 #14
ASM
lance wrote:
I have learned a bit more --

Using the bgColor attribute works fine in both IE and FF. The
difference between the two browsers is that when this attribute is
queried, the color reported is in different formats, e.g. 'orange' in
FF, and 'ffa500' in IE.
Yes and it's one of the problems.
Setting the bgColor attribute and then
querying the style.backgroundColor attribute does not work.
Normal : you don't speak about same things
It appears the using style.backgroundColor works almost equivalently as
long as you are consistent. The difference is that colors are reported
in the text format, e.g. 'orange', in both FF and IE.
interesting,
somebody from iCab told me that would not work in every browsers
if you use names instead colors codes
http://uia.dyndns.org/test2.html
http://uia.dyndns.org/test3.html


both works in my IE5.2, FF and Safari
only this with bgcolor works fine in iCab

but ...
I didn't see hovers on selected cells ;-)
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jukka K. Korpela | last post by:
I just noticed that most browsers render <table border="1"><tr><td><p>foo</p></td></tr></table> the same way as <table border="1"><tr><td>foo</td></tr></table> That is, they ignore the p...
0
by: Phil Evans | last post by:
URL: http://philevans.com/tabletest.html A boring table, but one which illustrates my problem. I'm writing a large PHP app which outputs data tables. Users of the app can configure their...
4
by: N. Demos | last post by:
The following code renders as intended in IE (A TABLE, with cells of fixed width and height, inside of a DIV with fixed width and height and overflow set to hidden.) In Firefox, the table cells...
0
by: N. Demos | last post by:
I have a single row table with fixed dimensioned cells nested inside a fixed dimensioned div, which has overflow: hidden. The div's dimensions are such that It should only display the first two...
3
by: N. Demos | last post by:
I have a single row table with fixed dimensioned cells nested inside a fixed dimensioned div, which has overflow: hidden. The div's dimensions are such that It should only display the first two...
2
by: Richard Brooks | last post by:
Basically, what I want to do is to have a viewport where an image is dropped in (hidden) on the right hand side and for the right hand side to be scrolled into view, when a key to go right is...
1
by: RobG | last post by:
I'm trying to get table cells to clip content rather than wrapping. It has been suggested to use: td.clipped { width: 5em; overflow: hidden; white-space: nowrap; }
2
by: markszlazak | last post by:
I'm a relatively slow response of table cells changing their background color with mouseover/our in IE6 (Win 2K) but the response is fine (fast) in Firefox. Why? The code is below. Sorry about the...
5
by: Test | last post by:
Is it possibel to have DIV's inside a table cell so that their position can be given relative to top left corner of the cell. Now the DIVs seem to position themselves relative to previous object.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.