PDA

View Full Version : finding TD position of TR? How?



BrightNail
Apr 11th, 2007, 01:09 AM
I have a TR with 4 columns (td's). Of course, this can change so I want a way to find the position of the TD in regards to its parent TR.

I have access to protoype and scriptaculous..but have not found what I am looking for in regards to a quick refrence

ala..

tr id="tr1"
td onclick="alert(document.getElementById('tr1').childNodes(this));"
td
td
td
/tr

any ideas? Basically, by onclicking, I want to be able to set the TD array position in regards to the PARENT tr. So, in that alert, I would hope to get "0"

glenngv
Apr 11th, 2007, 03:53 PM
function getCellPos(cell){
var cells = cell.parentNode.getElementsByTagName('td');
for (var i=0; i<cells.length; i++){
if (cells[i]==cell){
alert("Position is "+i);
break;
}
}
}


<table border="1">
<tr>
<td onclick="getCellPos(this)">1</td>
<td onclick="getCellPos(this)">2</td>
<td onclick="getCellPos(this)">3</td>
<td onclick="getCellPos(this)">4</td>
</tr>
<table>