Table Object: rows[] and cells[]

E

e271828

Is there a way to specify more than one row or cell collection in the
JS table object? Consider this:

var x=document.getElementById('myTable').rows[1].cells
x[0].innerHTML="NEW CONTENT"

This selects the second row (rows[1]) and the first cell (x[0]). How
would I select the first and second rows, and first - fifth rows, for
example? I would think something like the following:

var x=document.getElementById('myTable').rows[0,1].cells
x[0,1,2,3,4].innerHTML="NEW CONTENT"

or perhaps

var x=document.getElementById('myTable').rows[0-1].cells
x[0-4].innerHTML="NEW CONTENT"

But neither works. Any suggestions? A test example is here:
http://www.w3schools.com/js/tryit.asp?filename=try_dom_tablerow_cells.
 
M

Martin Honnen

e271828 wrote:

var x=document.getElementById('myTable').rows[0,1].cells
x[0,1,2,3,4].innerHTML="NEW CONTENT"

Write two nested loops then that do the job.
 
E

Evertjan.

Martin Honnen wrote on 20 jun 2006 in comp.lang.javascript:
e271828 said:
var x=document.getElementById('myTable').rows[0,1].cells
x[0,1,2,3,4].innerHTML="NEW CONTENT"

Write two nested loops then that do the job.

Write a function:

function fillCell(r,c,t){
var x=document.getElementById('myTable');
x.rows[r].cells[c].innerHTML = t;
};

test it:

fillCell(0,0,"NEW CONTENT");

and then go a-looping as Martin suggested if the above works.

for(var r=0;r<2;r++)
for(var c=0;c<5;c++)
fillCell(r,c,"NEW CONTENT");

not tested.
 

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

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top