I'm running into a problem more and more often, where I have an event handler tied to a <td>
, and I need to get the text in the first <td>
in that row (which is the primary key). What's the easiest way to do that, assuming I don't want to add any additional markup?
views:
193answers:
3
+1
A:
You would go to the parent <tr>
and then from the parent to the 1st child.
Ólafur Waage
2009-03-09 00:29:01
+4
A:
Using JQuery you could write something like this:
function eventHandler()
{
var firstTdText = $(this).parent().children("td:first").html();
}
Assuming of course, that "this" references the html TD.
Joel Potter
2009-03-09 00:32:29
that should be "td:first"
nickf
2009-03-09 00:40:47
Thanks. I should have tested it. I was thinking CSS selectors.
Joel Potter
2009-03-09 00:42:30
+2
A:
To reference the first cell in that row, you can select it using the prevAll
selector:
$(this).prevAll("td:last")
To get the text from it:
$(this).prevAll("td:last").html()
// or,
$(this).prevAll("td:last").text()
nickf
2009-03-09 00:36:15