If you are reading this, please get modern browser.
skip to main content | skip to main navigation | skip to secondary content

Detect Table Row Index with JavaScript

~ 18th May 2005. · 16:01 CET · permanent link · printer friendly ~

Sometimes it happens that you need exact table row index, so you can manipulate data in the HTML table on the client-side. Since the AJAX is so hot (very hot here in web.burza), it’s the matter of seconds ‘till someone will need this kind of information. The rowIndex property is far less known, but potentially useful method and it’s compatible with most of the browsers. Here is a quick example of its’ use.

<html>
<head>
<title>Row indexes</title>
<script type="text/javascript">
onload = function() {
    if (!document.getElementsByTagName || !document.createTextNode) return;
    var rows = document.getElementById('my_table').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
    for (i = 0; i < rows.length; i++) {
        rows[i].onclick = function() {
            alert(this.rowIndex + 1);
        }
    }
}
</script>
</head>
<body>
<table id="my_table">
<tbody>
    <tr><td>first row</td></tr>
    <tr><td>second row</td></tr>
</tbody>
</table>
</body>
</html>

Excuse me for not explaining it more deeply. After all, this is meant to be a quick reference for advanced Ajaxians. Feel free to develop discussion in the comments.

2 Comments

  1. Well, how about that! I didn’t know that there actually was a rowIndex property, silly me. I don’t really deal with tables a whole lot, so I’ve never needed this, but it’s good to know it’s around. I honestly need to start working with data tables and Ajax more often — lately I’ve been doing lots of Ajax, but nowhere near as much data table manipulation/processing. Thanks for this little bit.

  2. A good starting point in discovering DOM/JavaScript object properties and relations between document nodes is DOM inspector bundled with Firefox.

Sorry, the comment form is closed at this time, but if you have anything to say, please send me a message.

* Please keep in mind that this is a personal web site and it does not reflect the position or opinion of my respective employers, organizations or partners.

Typetester – compare screen type Supported by Veer.

What is this?

A web log of Marko Dugonjić, web professional from Croatia. Topics covered:

Translate this site

German, Spanish, Italian, French or Japanese (via).

See you there!

Feel like buying a book?

Try with maratz.com aStore

Worth visiting

top of the page | skip to main content | skip to main navigation | skip to secondary content