geomagnet Posted January 9, 2009 Share Posted January 9, 2009 I have a table with many elements, so to get to the row element allowing me to use the rowIndex method I get a really ugly statement:alert(objID(objName).parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.rowIndex);Is there a better way to do this...perhaps something like:alert(objID(objName).parentNode(*9).rowIndex); or alert(objID('table_in_question').rowIndex);Thanks Link to comment Share on other sites More sharing options...
aalbetski Posted January 9, 2009 Share Posted January 9, 2009 this little routine will do the trick. <table> <tr><td onclick="getRowIndex(this)">R1C1</td><td><span><div onclick="getRowIndex(this)">R1C2</div></span></tr> <tr><td onclick="getRowIndex(this)">R2C1</td><td><span><div onclick="getRowIndex(this)">R2C2</div></span></tr> <tr><td onclick="getRowIndex(this)">R3C1</td><td><span><div onclick="getRowIndex(this)">R3C2</div></span></tr> <tr><td onclick="getRowIndex(this)">R4C1</td><td><span><div onclick="getRowIndex(this)">R4C2</div></span></tr></table><script> function getRowIndex(object) { while (object.tagName != 'TR') { object = object.parentNode } alert (object.rowIndex) }</script> Link to comment Share on other sites More sharing options...
geomagnet Posted January 9, 2009 Author Share Posted January 9, 2009 this little routine will do the trick. <table> <tr><td onclick="getRowIndex(this)">R1C1</td><td><span><div onclick="getRowIndex(this)">R1C2</div></span></tr> <tr><td onclick="getRowIndex(this)">R2C1</td><td><span><div onclick="getRowIndex(this)">R2C2</div></span></tr> <tr><td onclick="getRowIndex(this)">R3C1</td><td><span><div onclick="getRowIndex(this)">R3C2</div></span></tr> <tr><td onclick="getRowIndex(this)">R4C1</td><td><span><div onclick="getRowIndex(this)">R4C2</div></span></tr></table><script> function getRowIndex(object) { while (object.tagName != 'TR') { object = object.parentNode } alert (object.rowIndex) }</script> Cool, that looks like a good solution for one table.How would you get this to work for nested tables and page with multiple tables?for instance:<table id="table_1"><tr><td> stuff</td><tr><tr><td> stuff</td><tr><tr><td> stuff</td><tr><tr><td> stuff</td><tr><tr><td> stuff</td><tr><tr><td> [indent] <table id = "table_2"><tr><td> stuff</td><tr> <tr><td> stuff</td><tr><tr><td> stuff</td><tr><tr><td> stuff</td><tr>[/indent]</table></td><tr></table> Link to comment Share on other sites More sharing options...
aalbetski Posted January 14, 2009 Share Posted January 14, 2009 doesn't matter. The function stops when it encounters the first TR tag. If you need it to be a TR tag of a specific table, then you will have to test for that. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.