View Full Version : row count - what is the name for a row
Marcus Dunkus
03-02-2005, 05:13 PM
I have this bit of script:
rowCount = document.getElementById('inventory').getElementsByTagName('tr').length;
for(x=0;x<rowCount;x++){......
So I can iterate thru a table row-by-row but I don't know how to use that script to identify the row I'm in.
I would like to then write: row(x).getElementsByTagName('input')
or just: getElementsByTagName('input')
but that doesn't work, javascript expects an object.
Any ideas?
Thanks
Mark
glenngv
03-03-2005, 04:22 AM
var rows = document.getElementById('inventory').getElementsByTagName('tr');
var rowCount = rows.length;
var inputs, inputCount;
for(x=0;x<rowCount;x++){
alert(rows[x].id); //if you provide id for each row
inputs = rows[x].getElementsByTagName('input');
inputCount = inputs.length;
for(y=0;y<inputCount;y++){
alert(inputs[y].value);
}
}
If the number of inputs per row is fixed to something (say 2), then you don't have to do the inner loop. Just specify inputs[0].value and inputs[1].value.
Marcus Dunkus
03-03-2005, 10:08 AM
Ahh you make it all look so easy!
This will be really useful.
Thanks a million.
check out 'close but no cigar...' for the rest of the story.
Mark
So I can iterate thru a table row-by-row but I don't know how to use that script to identify the row I'm in.
It is a metter of parent(s) chain length to refere the row starting from a child element. The key is this self reference pased as a parameter.
<table id="tab">
...
<tr>
<td><input type="button" value="alert the index of this row" onclick="alertR(this)"></td>
</tr>
...
</table>
function alertR(theButton){
var theRow = theButton.parentNode.parentNode;//the row; 1st parent is the cell, 2nd is the row
var allRows = document.getElementById('tab').getElementsByTagName('tr');//the rows collection
for(var i=0;i<allRows.length;i++){
if(theRow==allRows[i]){//compare the two objects
alert('the row index is: '+i);//if bitwise boolean is true, alert the index
break;//stop the further useless countdown in loop
}
}
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.