Using JQuery to check if no radio button in a group has been checked

Jquery

Jquery Problem Overview


I'm sitting with a problem, I need to check with JQuery if no radio button within a radio button group has been checked, so that I can give the users an javascript error if they forgot to check a option.

I'm using the following code to get the values

var radio_button_val = $("input[name='html_elements']:checked").val();

Jquery Solutions


Solution 1 - Jquery

if (!$("input[name='html_elements']:checked").val()) {
   alert('Nothing is checked!');
}
else {
  alert('One of the radio buttons is checked!');
}

Solution 2 - Jquery

I'm using

$("input:radio[name='html_radio']").is(":checked")

And will return FALSE if all the items in the radiogroup are unchecked and TRUE if an item is checked.

Solution 3 - Jquery

You could do something like this:

var radio_buttons = $("input[name='html_elements']");
if( radio_buttons.filter(':checked').length == 0){
  // None checked
} else {
  // If you need to use the result you can do so without
  // another (costly) jQuery selector call:
  var val = radio_buttons.val();
}

Solution 4 - Jquery

if ($("input[name='html_elements']:checked").size()==0) {
   alert('Nothing is checked!');
}
else {
  alert('One of the radio buttons is checked!');
}

Solution 5 - Jquery

Use .length refer to http://api.jquery.com/checked-selector/

if ($('input[name="html_elements"]:checked').length === 0) alert("Not checked");
else alert("Checked");

Solution 6 - Jquery

var len = $('#your_form_id input:radio:checked').length;
      if (!len) {
        alert("None checked");
      };
      alert("checked: "+ len);

Solution 7 - Jquery

I think this is a simple example, how to check if a radio in a group of radios was checked.

if($('input[name=html_elements]:checked').length){
    //a radio button was checked
}else{
    //there was no radio button checked
} 

Solution 8 - Jquery

I am using this much simple

HTML

<label class="radio"><input id="job1" type="radio" name="job" value="1" checked>New Job</label>
<label class="radio"><input id="job2" type="radio" name="job" value="2">Updating Job</label>


<button type="button" class="btn btn-primary" onclick="save();">Save</button>

SCRIPT

 $('#save').on('click', function(e) {
	if (job1.checked)
    	{
  	          alert("New Job"); 
        }
if (job2.checked)
        {
            alert("Updating Job");
        }

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElitmiarView Question on Stackoverflow
Solution 1 - JqueryDominic RodgerView Answer on Stackoverflow
Solution 2 - JqueryJacobo HernándezView Answer on Stackoverflow
Solution 3 - JqueryDoug NeinerView Answer on Stackoverflow
Solution 4 - JqueryfanjabiView Answer on Stackoverflow
Solution 5 - JqueryN ZhangView Answer on Stackoverflow
Solution 6 - JqueryDavid OkwiiView Answer on Stackoverflow
Solution 7 - JqueryVitali D.View Answer on Stackoverflow
Solution 8 - JqueryMathew MaganteView Answer on Stackoverflow