Select first sibling

I would set up an event using jQuery, but that's totally up to you.

$("table td:last img").click(function() {
    var firstTd = $(this).parents("tr").find("td:first").html();
    alert("you clicked person #" + firstTd);
}); 

No matter what, you can still use this example with your own code:

function doSomething()
{
    var firstTd = $(this).parents("tr").find("td:first-child").html();
    alert("you clicked person #" + firstTd);
}

You're right. 'this' is not being passed, you need to edit the html to make that work. Or just use jQuery instead as show above.


$( 'td:first-child', $( this ).parents ( 'tr' ) ).html ();

This will select the first TD element (:first-child filter) in the parent TR of the image. parents() returns all parents of the element, and we filter the parents so that only TR elements are returned.

Also try writing your image like so:

<img src="bobsmith.png" onclick="doSomething(this)" />

and your function like so:

function doSomething ( imgEl ) {
}

and use imgEl instead of this


$('tr td:last-child img').click(function(){

    alert($('td:first-child',$(this).closest('tr')).text());

});