Wednesday, March 30, 2005

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: ,

2 Comments:

Anonymous Anonymous said...

Doesn't childNodes[] include whitespace nodes in some browsers? Does this work with whitespace in Firefox?

11:26 PM  
Blogger cyfer said...

By using if( "TD" == vRow.childNodes[ i ].nodeName ), I only select the cell begin with "TD".

10:27 PM  

Post a Comment

<< Home