views:

337

answers:

7

I have an HTML table having n rows and each rows contain one radiobutton in the Row.Using jQuery , How can i look thru these radio buttons to check which one is checked ?

+3  A: 
$('#table tbody tr input[type=radio]').each(function(){
 alert($(this).attr('checked'));
});

HTH.

Sunny
`#table > tr` will choke on certain browsers (chrome/firefox) that like to add their own `tbody` elements, whereas `#table tr` won't.
karim79
@karim79 - modified
Sunny
You could also use the `:radio` selector (http://api.jquery.com/radio-selector/)
fudgey
+3  A: 
$('.my-radio-class:checked')

http://api.jquery.com/checked-selector/

Jarrett Meyer
+3  A: 

Do you want to process every radio button or do you only need the checked ones? If the latter, it is quite easy:

$('table input:radio:checked')

Reference: :radio, :checked

Felix Kling
+1. Afaik jQuery's look-up goes from right to left, so the whole `thead tr td` is unnecessary if the only condition is that they need to be in a table. And `:radio` and `:checked` are the most ‘elegant’ selectors in this case.
Alec
+1  A: 
$("table tr input[type=radio]:checked");
Jon
+3  A: 

There are many ways to do that, e.g., using .each and the .is traversal method:

$("table tr td input[name=something]:radio").each(function() {
    if($(this).is(":checked")) {
        $(this).closest("tr").css("border", "1px solid red");
    } else {
        // do something else
    }
});
karim79
@karim79 - I'll give +1 if you can tell me if it is pretty well guaranteed that all (jQuery supported) browsers will reliably insert a missing `<tbody>`.
patrick dw
@patrick - No, that's not the case. Which means that my selector is broken some of the time. Which means I'll edit this answer now. Damn it, but thanks, I should pay more attention to my own answers in the future :)
karim79
Thanks for the response. +1 for corrected answer.
patrick dw
+2  A: 
var checked = $('#table :radio:checked');
PetersenDidIt
+1  A: 
//get the checked radio input, put more specificity in the selector if needed
var $checkedRadio = $("input[type=radio]:checked");

//if you want the value of the checked radio...
var checkedRadioVal = $checkedRadio.val();
Scott Christopherson