this.rowIndex question

W

webdev

Folks,

Can someone please tell me why this simple piece of code, irritatingly,
fails to give the expected result?

Regards
Rae MacLeman

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Sample Code</title>
</head>
<body>
<table id="table1" border="1">
<tr>
<td onClick="alert(this.rowIndex);">Tell me which row</td>
</tr>
<tr>
<td onClick="alert(this.rowIndex);">Tell me which row</td>
</tr>
<tr>
<td onClick="alert(this.rowIndex);">Tell me which row</td>
</tr>
</table>
</body>
</html>
 
M

Michael Winter

Can someone please tell me why this simple piece of code, irritatingly,
fails to give the expected result?
[snip]

<td onClick="alert(this.rowIndex);">Tell me which row</td>

[snip]

Table cells don't have a rowIndex property, rows do.

onclick="alert(this.parentNode.rowIndex);"

retrieves the containing row, and its index within the table.

Mike
 
R

Richard Cornford

webdev said:
Can someone please tell me why this simple piece of code,
irritatingly, fails to give the expected result?

Not unless you state what your expectation is. I would be expecting the
alerts to report "undefined" or fail to operate (having no string
argument) depending on the browser.

<tr>
<td onClick="alert(this.rowIndex);">Tell me which row</td>
</tr>
<snip>

Are you expecting the TD elements to have a rowIndex property? If you
are interested in the rowIndex of the containing TR you might try -
this.parentNode.rowIndex -.

Richard.
 
W

webdev

Richard Cornford said:
Not unless you state what your expectation is. I would be expecting the
alerts to report "undefined" or fail to operate (having no string
argument) depending on the browser.


<snip>

Are you expecting the TD elements to have a rowIndex property? If you
are interested in the rowIndex of the containing TR you might try -
this.parentNode.rowIndex -.

Richard.
My expectation was that it would return the rowindex ;0)

I'm wiser now
 
W

webdev

Michael Winter said:
Can someone please tell me why this simple piece of code, irritatingly,
fails to give the expected result?
[snip]

<td onClick="alert(this.rowIndex);">Tell me which row</td>

[snip]

Table cells don't have a rowIndex property, rows do.

onclick="alert(this.parentNode.rowIndex);"

retrieves the containing row, and its index within the table.

Mike

Thanks Mike - I'd already tried "parentNode.rowIndex" somewhere along the
line.

Rae
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top