Tuesday, November 23, 2010

Getting element from DOM tree

If we want to get a cell element from a table, we would get the id of the table first. With the ID then we will navigate through the rows and cols to get to the cell.

A simple way to get this is

mycell = tblid.childNodes[x].childNodes[w]

Unfortunately, it is not that simple. Usually when people write html, they tends to spread out the tags in different lines for easier visibility. Often you would do this

<tr>
<td>text<td>
<tr>

In IE you would not have a problem as IE would ignore the whitespaces. However, other browsers are more specific. They will not ignore the whitespaces.

If you use the same simple code, you will not get the element that you wanted. You would have to find a way to ignore the whitespaces like IE.

The following is a simple code to do it.

function getcell(tbdid,params){
var z;
var zz;
addEval=tbdid
for (y=0;y<params.length;y++){
zz=-1
for (z=0;z<params[y];z++){
if (addEval.childNodes[z].nodeType != 1)
zz +=1
zz+=1
}
addEval=addEval.childNodes[zz]
}
return addEval
}
mycell=getcell(tblid,[x,w])

Note that x and w are numbers starting with 1 instead of 0.

No comments:

Post a Comment