Using this function. But it adds row_selected class to all rows. How can I add it, only to parent row?
$('.checkbox').click( function() {
if ( $("#list tr").hasClass('row_selected') )
$("#list tr").removeClass('row_selected');
else
$("#list tr").addClass('row_selected');
} );
You can use the
.closest() methodto find the nearest parent (or grandparent/ancestor) element matching a particular selector. So try this:Note that the
.toggleClass() methodtakes care of your if/else for you, but if you really want to code it manually:Note that either way, in a jQuery event handler
thisis the element the event was triggered on, so you can manipulate it or find its parents/siblings/whatever by getting a jQuery object with$(this).