Jquery – Getting the id of a table row when a checkbox is checked

jquery

I have a checkbox inside a table row

<tr id="1">
<td><input type="checkbox" /></td>
<td>lorem</td>
<td>ipsum</td>
<td>css</td>
</tr>

I want to get the id when the checkbox is checked

I have tried this

var closestTr = $(':checkbox:checked').closest('tr').attr('id');

alert(closestTr);

I also tried this http://jsfiddle.net/AbfJk/3/

Why doesn't it get the id?

Best Solution

try this

$("#thechecked").click(function(){
    var closestTr = $(':checkbox:checked').closest('tr').attr('id');
    alert(closestTr);
});

working fiddle: http://jsfiddle.net/AbfJk/7/

anyway, your fiddle is missing jquery and has also a ")" missing (you put the "end comment" in the wrong line)