This works in IE but not in Mozilla

M

Martin

I have some functions in a script in which I'm manipulating the
innerText and background colors of certain rows in a table. The lines
below work OK in IE but when I try them in Mozilla, I get an error
that says: "document.getElementById('TableX').rows is not a
function".

thisRow = document.getElementById('TableX').rows(1);
thisRow.style.background = 'white';

Can anyone give me a clue as to how to fix this so that it will work
in both browsers?

Thanks.
 
D

DU

Martin said:
I have some functions in a script in which I'm manipulating the
innerText


Mozilla and Firefox do not support innerText. Use DOM 3 textContent()
instead for Mozilla and Firefox.

and background colors of certain rows in a table. The lines
below work OK in IE but when I try them in Mozilla, I get an error
that says: "document.getElementById('TableX').rows is not a
function".

thisRow = document.getElementById('TableX').rows(1);

() identifies parameters of a function while [] identifies the accessor
of an array.
So
thisRow = document.getElementById('TableX').rows[1];
will work in both browsers.
thisRow.style.background = 'white';

thisRow.style.backgroundColor = 'white';
Can anyone give me a clue as to how to fix this so that it will work
in both browsers?

Thanks.

DU
 
R

RobG

Martin said:
I have some functions in a script in which I'm manipulating the
innerText and background colors of certain rows in a table. The lines
below work OK in IE but when I try them in Mozilla, I get an error
that says: "document.getElementById('TableX').rows is not a
function".

thisRow = document.getElementById('TableX').rows(1);
thisRow.style.background = 'white';

The rows collection is like a reduced functionality array, you must
address its members the same as elements in an array:

thisRow = document.getElementById('TableX').rows[1];

And the correct way to modify an element's background color using its
style object is:

this.style.backgroundColor = 'white';

You should also do feature detection before using either
getElementById or style:

if (document.getElementById) {
thisRow = document.getElementById('TableX').rows[1];
}
if (thisRow.style) {
thisRow.style.backgroundColor = 'white';
}

Of course you only need to test for these once, and it may be helpful
to provide alternative methods for browsers that don't support them.

Have a look at the group FAQ for help on this and the square/round
bracket issue.

Can anyone give me a clue as to how to fix this so that it will work
in both browsers?

As was noted in DU's post, innerText will not work in
Firefox/Mozilla (or most browsers other than IE).

There are some suggestions in the thread below on cross-browser
implementations of innerText functionality (please excuse wrapping)
or search for 'innerText' in this newsgroup on 27 March 2005:


<URL:http://groups-beta.google.com/group...594350a25?q=innerText&rnum=2#ed976a3594350a25>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top