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
2010-05-17 19:23:47
`#table > tr` will choke on certain browsers (chrome/firefox) that like to add their own `tbody` elements, whereas `#table tr` won't.
karim79
2010-05-17 19:26:15
@karim79 - modified
Sunny
2010-05-17 19:29:04
You could also use the `:radio` selector (http://api.jquery.com/radio-selector/)
fudgey
2010-05-18 00:13:00
+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')
Felix Kling
2010-05-17 19:24:45
+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
2010-05-18 01:31:33
+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
2010-05-17 19:25:29
@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
2010-05-17 19:40:14
@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
2010-05-17 19:44:23
+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
2010-05-17 19:26:32