views:

41

answers:

2

Hi All,

I have a table (standard markup) with a radio select in each row. Once the radio is selected I'd like to highlight that . Sounds straight forward enough but I can't get it to trigger.

Here's the markup:

The Table Row:

<tr>
<td>some data</td>
<td>some data</td>
<td>some data</td>
<td>
<label class="label_radio"><input type="radio" name="ame" value="val" /></label>
</td>
</tr>

This is the relevant part of the JS: (mods the label to sexify the radio button, that bit works, the bit doesn't):

$('.label_radio input:checked').each(function(){ 
$(this).parent('label').addClass('r_on');
$(this).parent('tr').addClass('.hilite'); //this line doesn't work
});

Any ideas? If I haven't given enough intel, please say and I'll get you what you need.

Greatly appreciated in advance :)

+3  A: 

The radio's parent isn't actually the table row, it's the label. use the .closest method instead to look up the parent chain until you get to the TR.

$(this).closest('tr').addClass('hilite');
BBonifield
Cleaner than mine, and faster. Out of upvotes tho, sorry :)
Litso
NOTE: I just edited my answer because the original author was doing .addClass('.hilite') instead of .addClass('hilite'). I rather doubt he meant to do that.
BBonifield
I am in clearly having one of those days! Yep, you're right both counts. My mistake in the '.hilite', I had already corrected that. Thank you so much - I can't beleive I didn't see the 'parent' issue before asking for help.
Tim
And thank you too for all your prompt replies! Fixed!
Tim
Or he can use .parents() which will find the first hierarchy upwards.
Tudorizer
@Tudorizer: Using .parents() isn't actually the same. It'll fetch all of the parent elements, even ones past the TR that he was trying to target. While .parents('tr:first') would return the same element, it wouldn't be as efficient.
BBonifield
@BBonifield, gotcha
Tudorizer
A: 

Not the cleanest way probably, but what if you use $(this).parent().parent().addClass('.hilite'); ?

Litso