How do I loop through a radio buttons group without a form in JavaScript or jQuery?
A:
I can't be too sure what you mean but if you want to do something to all radio buttons on a page you can do this:
$("input:radio").each(function(){
//do something here
});
Razor Storm
2010-08-13 00:47:53
The selector "radio" will return nothing.There's no element (tag) called "radio" in HTML.It's <input type="radio">
Topera
2010-08-13 00:52:02
$(':radio') is equivalent to $('[type=radio]'). http://api.jquery.com/radio-selector/
Dumb Guy
2010-08-13 00:55:43
@Dumb Guy: I think @Topera was referring to a previous edit, which was fixed by @Razor Storm
Daniel Vassallo
2010-08-13 01:03:12
Razor Storm
2010-08-13 01:03:45
A:
What about something like this? (using jQuery):
$('input:radio').each(function() {
if($(this).is(':checked')) {
// You have a checked radio button here...
}
else {
// Or an unchecked one here...
}
});
You can also loop through all the checked radio buttons like this, if you prefer:
$('input:radio:checked').each(function() {
// Iterate through all checked radio buttons
});
Daniel Vassallo
2010-08-13 00:48:41