views:

3765

answers:

3

i have two radio buttons within a form. a dialog box appears when one of the fields is null. how can i check if a radio button is selected?

+7  A: 

Let's pretend you have HTML like this

<input type="radio" name="gender" id="gender_Male" value="Male" />
<input type="radio" name="gender" id="gender_Female" value="Female" />

For client-side validation, here's some Javascript to check which one is selected:

if(document.getElementById('gender_Male').checked) {
  //Male radio button is checked
}else if(document.getElementById('gender_Female').checked) {
  //Female is checked
}

The above could be made more efficient depending on the exact nature of your markup but that should be enough to get you started.


If you're just looking to see if any radio button is selected anywhere on the page, PrototypeJS makes it very easy.

Here's a function that will return true if at least one radio button is selected somewhere on the page. Again, this might need to be tweaked depending on your specific HTML.

function atLeastOneRadio() {
    return ($$('input[type=radio]:checked').size() > 0);
}


For server-side validation (remember, you can't depend entirely on Javascript for validation!), it would depend on your language of choice, but you'd but checking the gender value of the request string.

Mark Biek
but what i want to check if a radio button is selected regardless of what is selected.
noob
I don't really follow what you're saying? Are interested in whether or not ANY radio button is selected?
Mark Biek
yes. because the form cannot be submitted if not all the fields are filled-in including the radio buttons.
noob
if (document.getElementById('gender_Male').checked || document.getElementById('gender_Female').checked) alert('some of my radioboxes is checked');
Havenard
I've modified my Prototype example to take advantage of the CSS selector lesson I just learned from R. Bemrose.
Mark Biek
+1  A: 

With jQuery, it'd be something like

if ($('input[name=gender]:checked').length > 0) {
    // do something here
}

Let me break that down into pieces to cover it more clearly. jQuery processes things from left to right.

input[name=gender]:checked
  1. input limits it to input tags.
  2. [name=gender] limits it to tags with the name gender within the previous group.
  3. :checked limits it to checkboxes/radio buttons that are selected within the previous group.

If you want to avoid this altogether, mark one of the radio buttons as checked (checked="checked") in the HTML code, which would guarantee that one radio button is always selected.

R. Bemrose
+1 for some CSS selector goodness that I didn't know about.
Mark Biek
+3  A: 

A vanilla JavaScript way

var radios = document.getElementsByTagName('input');
var value;
for (var i = 0; i < radios.length; i++) {
    if (radios[i].type === 'radio' && radios[i].checked) {
        // get value, set checked flag or do whatever you need to
        value = radios[i].value;       
    }
}
Russ Cam
Well i got almost the same problem right now and your code was very helpfull
MoreThanChaos