4

I have found scripts that do it, but they only work with one radio button name, i have 5 different radio button sets. How can i check if its selected right now i tried on form submit

if(document.getElementById('radiogroup1').value=="") {
        alert("Please select option one");
        document.getElementById('radiogroup1').focus();
        return false;
    }

does not work.

Jonas
  • 121,568
  • 97
  • 310
  • 388
JohnA
  • 1,875
  • 8
  • 28
  • 34
  • Possible duplicate of [How can I check whether a radio button is selected with JavaScript?](http://stackoverflow.com/questions/1423777/how-can-i-check-whether-a-radio-button-is-selected-with-javascript) – Siraj Alam Mar 14 '17 at 18:09

6 Answers6

6

If you have your heart set on using standard JavaScript then:

Function definition

var isSelected = function() {
    var radioObj = document.formName.radioGroupName;

    for(var i=0; i<radioObj.length; i++) {
        if( radioObj[i].checked ) {
            return true;
        }
    }

    return false;
};

Usage

if( !isSelected() ) {
    alert('Please select an option from group 1 .');
}   

I'd suggest using jQuery. It has a lot of selector options which when used together simplify the much of the code to a single line.

Alternate Solution

if( $('input[type=radio][name=radioGroupName]:selected').length == 0 ) {
    alert('Please select an option from group 1 .');
}
Oliver
  • 421
  • 3
  • 6
  • 3
    This answer is **ancient** (in web years), so be aware the [jQuery is not the best solution for selecting elements](http://blog.garstasio.com/you-dont-need-jquery/selectors/) now that IE < 8 are all dead. – BryanH Mar 07 '16 at 20:15
2
var checked = false, radios = document.getElementsById('radiogroup1');
for (var i = 0, radio; radio = radios[i]; i++) {
    if (radio.checked) {
        checked = true;
        break;
    }
}

if (!checked) {
    alert("Please select option one");
    radios.focus();
    return false;
}

return true;
Kevin
  • 1,147
  • 11
  • 21
  • 3
    By specification, no two elements on a page should have the same id. – Mar Mar 01 '13 at 21:03
  • 1
    `document.getElementsById()` is wrong. [**s** after `Element`]. It should be `document.getElementById()` OR `document.getElementsByClassName[]()` – Siraj Alam Mar 14 '17 at 18:03
1

Why don't just use a oneliner?

I wrote this code, it will submit the form if at least one radio is checked:

(function(el){for(var i=el.length;i--;) if (el[i].checked) return el[i].form.submit()||1})(document.form_name.radio_name)||alert('please select item')

Otherwise it will make an alert. Or you may also modify it to use with form's onsubmit:

return (function(el){for(var i=el.length;i--;) if (el[i].checked) return 1})(document.form_name.radio_name)||alert('please select item')

Just replace form_name and radio_name accordingly.

See how it works: http://jsfiddle.net/QXeDv/5/

Anonymous
  • 4,692
  • 8
  • 61
  • 91
  • I know this is an old one, but writing all of your code on one line doesn't make your code a 'one liner'; avoid this practice. – muhr Dec 21 '15 at 15:47
  • You are actually wrong. They are oneliners that can work inline. Please see the fiddle to learn how they work. If you cannot understand what it does... then it's better to restrain from commenting. – Anonymous Jun 11 '16 at 08:07
1

Here's a good tutorial -> http://www.somacon.com/p143.php


// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
    if(!radioObj) return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked) return radioObj.value;
        else return "";
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) return radioObj[i].value;
    }
    return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
    if(!radioObj) return;
    var radioLength = radioObj.length;
    if(radioLength == undefined) {
        radioObj.checked = (radioObj.value == newValue.toString());
        return;
    }
    for(var i = 0; i < radioLength; i++) {
        radioObj[i].checked = false;
        if(radioObj[i].value == newValue.toString()) radioObj[i].checked = true;
    }
}
SpYk3HH
  • 22,272
  • 11
  • 70
  • 81
1

A very simple function is:

<script type="text/javascript">

function checkRadios(form) {
   var btns = form.r0;
   for (var i=0; el=btns[i]; i++) {
     if (el.checked) return true;
   }
   alert('Please select a radio button');
   return false;
}
</script>

<form id="f0" onsubmit="return checkRadios(this);">
  one<input type="radio" name="r0"><br>
  two<input type="radio" name="r0"><br>
  three<input type="radio" name="r0"><br>
  <input type="submit">
</form>

However, you sould always have one radio button selected by default (i.e. with the select attribute), some user agents may automatically select the first button. Then you just need to check if the default (usually the first one) is checked or not.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

Are you ok with jquery? If so:

$(document).ready(function(){
    if($('input[type=radio]:checked').length == 0)
    {
        alert("Please select option one");         
        document.getElementById('radiogroup1').focus();         
        return false;     
    }
}
Zen Pak
  • 578
  • 6
  • 22
  • `$(document).ready(function(){` made easier in jQuery with `$(function(){`. They both the same thing except the later is newer and runs through more cross browser filtering for doc reday check and fits jquey's style better – SpYk3HH Feb 16 '12 at 04:34
  • also, the if statement is made even esier in jQuery with a simple jquery style call `if ($('input.classnameIsEasier').is(':checked')) { /* do work */ };` – SpYk3HH Feb 16 '12 at 04:38
  • and one more thing, if you're gonna use jquery and need a form validation for radios and checks and such, go with [this plugin](http://jquery.malsup.com/form/) great for ajaxing your form for more functionality and better validation timing – SpYk3HH Feb 16 '12 at 04:40