Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through a radio buttons group without a form?

Tags:

How do I loop through a radio buttons group without a form in JavaScript or jQuery?

like image 489
Bart Avatar asked Aug 13 '10 00:08

Bart


People also ask

How do I tab through a radio button?

When focus moves to the group in which a radio button is selected, pressing Tab and Shift+Tab keys move focus to the radio button that is checked. Up Arrow and Down Arrow keys move focus and selection. Up Arrow key press moves focus and selection forward through the radio buttons in the group.

How do I group set radio buttons?

You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.

How do you make a radio button unselectable?

Answer: To make a radio button not selectable, in the button's INPUT tag you can use an onclick event handler like this: <INPUT type="radio" name="myButton" value="theValue" onclick="this. checked=false; alert('Sorry, this option is not available!') ">

How many radio buttons you can select at a time from one group?

Only one radio button in a given group can be selected at the same time.


2 Answers

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 }); 
like image 103
Daniel Vassallo Avatar answered Nov 12 '22 21:11

Daniel Vassallo


...in case someone wants to do this without jQuery (since it was part of the question):

I'm not sure what you mean by without a form. If you mean you don't want to pass the form element to a javascript function, you could do it like this:

for (var i = 0; i < document.form_name.radio_name.length; i++) {     if (document.form_name.radio_name[i].checked) {         // ...     } } 

If you mean without a form as in you have no form node, you could wrap them in a span (or a div) and use code like this:

var span = document.getElementById("span_id"); var inputs = span.getElementsByTagName("input"); for (var i = 0; i < inputs.length; ++i) {     if (inputs[i].checked) {         // ...     } } 
like image 31
bmaupin Avatar answered Nov 12 '22 19:11

bmaupin