tags:

views:

193

answers:

3

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?

+1  A: 

You would go to the parent <tr> and then from the parent to the 1st child.

Ólafur Waage
+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
that should be "td:first"
nickf
Thanks. I should have tested it. I was thinking CSS selectors.
Joel Potter
+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