Javascript code to access table cell via id
function getTableCell(id, row, cell){
    var vRow = document.getElementById( id ).rows[ row ];
    var i=0, j=0;
    for(;i < vRow.childNodes.length; i ++ ){
     if( "TD" == vRow.childNodes[ i ].nodeName ){
      if( j == cell ) return vRow.childNodes[ i ];
      j ++;
     }
    }
    return null;
   }
Tested on Firefox 1.02, IE6, Safari 1.2
Labels: javascript, table cell

2 Comments:
Doesn't childNodes[] include whitespace nodes in some browsers? Does this work with whitespace in Firefox?
By using if( "TD" == vRow.childNodes[ i ].nodeName ), I only select the cell begin with "TD".
Post a Comment
<< Home