Reputation: 1353
I've a table containing a number of rows, where each row contains 3 td elements, the last of which is always a radio button.
I've a jquery function which is fired when any of these radio buttons is checked:
$('input[type=radio]').change(function() {
$('input[type=radio]').each(function(index) {
//remove class from tr
});
//add class selected to tr
});
What I'm looking to do is change the class of the tr containing the selected radio button to 'selected' and remove the class 'selected' from the previous selected option (for highlighting). Is there any way to access the tr from the radio button?
Upvotes: 1
Views: 1442
Reputation: 382726
Try this:
$('input[type=radio]').change(function() {
$('input[type=radio]').each(function(index) {
$(this).closest('tr').removeClass('selected');
});
$(this).closest('tr').addClass('selected');
});
Note that you could make it shorter with $(':radio')
rather than $('input[type=radio]')
.
More Info:
Upvotes: 1
Reputation: 36373
Well this can be done simply by removing class selected
from all tr, and then adding where we want. Simple eh..
$('input[type=radio]').change(function() {
$(this).parents('table').find('tr').removeClass('selected');
$(this).parents('tr:first').addClass('selected');
});
Upvotes: 0