views:

87

answers:

2

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
The selector "radio" will return nothing.There's no element (tag) called "radio" in HTML.It's <input type="radio">
Topera
$(':radio') is equivalent to $('[type=radio]'). http://api.jquery.com/radio-selector/
Dumb Guy
@Dumb Guy: I think @Topera was referring to a previous edit, which was fixed by @Razor Storm
Daniel Vassallo
Razor Storm
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