Table Object: rows[] and cells[]

Discussion in 'Javascript' started by e271828, Jun 20, 2006.

  1. e271828

    e271828 Guest

    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:
    e271828, Jun 20, 2006
    1. Advertisements

  2. e271828 wrote:

    Write two nested loops then that do the job.
    Martin Honnen, Jun 20, 2006
    1. Advertisements

  3. e271828

    Evertjan. Guest

    Martin Honnen wrote on 20 jun 2006 in comp.lang.javascript:
    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.
    Evertjan., Jun 20, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.