Set selected radio from radio group with a value

Jquery

Jquery Problem Overview


Why am I struggling with this?

I have a value: 5

How do I check the radio button of group "mygroup" with the value of 5?

$("input[name=mygroup]").val(5); // doesn't work?

Jquery Solutions


Solution 1 - Jquery

With the help of the attribute selector you can select the input element with the corresponding value. Then you have to set the attribute explicitly, using .attr:

var value = 5;
$("input[name=mygroup][value=" + value + "]").attr('checked', 'checked');

Since jQuery 1.6, you can also use the .prop method with a boolean value (this should be the preferred method):

$("input[name=mygroup][value=" + value + "]").prop('checked', true);

Remember you first need to remove checked attribute from any of radio buttons under one radio buttons group only then you will be able to add checked property / attribute to one of the radio button in that radio buttons group.

Code To Remove Checked Attribute from all radio buttons of one radio button group -

$('[name="radioSelectionName"]').removeAttr('checked');

Solution 2 - Jquery

There is a better way of checking radios and checkbox; you have to pass an array of values to the val method instead of a raw value

Note: If you simply pass the value by itself (without being inside an array), that will result in all values of "mygroup" being set to the value.

$("input[name=mygroup]").val([5]);

Here is the jQuery doc that explains how it works: http://api.jquery.com/val/#val-value

And .val([...]) also works with form elements like <input type="checkbox">, <input type="radio">, and <option>s inside of a <select>.

> The inputs and the options having a value that matches one of the elements of the array will be checked or selected, while those having a value that don't match one of the elements of the array will be unchecked or unselected

Fiddle demonstrating this working: https://jsfiddle.net/92nekvp3/

Solution 3 - Jquery

Try this:

$('input:radio[name="mygroup"][value="5"]').attr('checked',true);

JS Fiddle demo.

Solution 4 - Jquery

$("input[name='mygroup'][value='5']").attr("checked", true);

Solution 5 - Jquery

When you change attribute value like mentioned above the change event is not triggered so if needed for some reasons you can trigger it like so

$('input[name=video_radio][value="' + r.data.video_radio + '"]')
       .prop('checked', true)
       .trigger('change');

Solution 6 - Jquery

$("input[name='RadioTest'][value='2']").prop('checked', true);

JS fiddle Demo

Solution 7 - Jquery

Pure JavaScript version:

document.querySelector('input[name="myradio"][value="5"]').checked = true;

Solution 8 - Jquery

Or you can just write value attribute to it:

$(':radio[value=<yourvalue>]').attr('checked',true);

This works for me.

Solution 9 - Jquery

I got an error when using

$("input[name=mygroup][value="value"]").prop('checked', true);

Working way is

$("input[name=mygroup][value='value']").prop('checked', true);

The issue is handling quotes(') and double quotes(").

Solution 10 - Jquery

var key = "Name_radio";
var val = "value_radio";
var rdo = $('*[name="' + key + '"]');
if (rdo.attr('type') == "radio") {
 $.each(rdo, function (keyT, valT){
   if ((valT.value == $.trim(val)) && ($.trim(val) != '') && ($.trim(val) != null))

   {
     $('*[name="' + key + '"][value="' + (val) + '"]').prop('checked', true);
   }
  })
}



 

Solution 11 - Jquery

First way assumes there is already a saved reference for the radio group (inputs): group and it is wished to select one in the group, by value

const group = document.querySelectorAll('input[name="contact"]');

function selectGroupByValue(group, value){
  const input = [...group].find(el => el.value === value);
  if(input) input.checked = true;
}

selectGroupByValue(document.querySelectorAll('input[name="contact"]'), 'phone');

<input type="radio" name="contact" value="email">
<input type="radio" name="contact" value="phone">
<input type="radio" name="contact" value="mail">

This can be taken a step further, to create a more robust function which can change the value of a any group, by its name:

function selectGroupByValue(name, value){
  const group = document.querySelectorAll(`input[name="${name}"]`);
  const input = [...group].find(el => el.value === value);
  if(input) input.checked = true;
}

selectGroupByValue('contact', 'phone');

<input type="radio" name="contact" value="email">
<input type="radio" name="contact" value="phone">
<input type="radio" name="contact" value="mail">

The below example is completely different as it does not have a cached reference to the radio group, but does a lookup every time:

const selectedValue = 'phone';
const input = document.querySelector(`input[name="contact"][value="${selectedValue}"`);
if(input) input.checked = true;

<input type="radio" name="contact" value="email">
<input type="radio" name="contact" value="phone">
<input type="radio" name="contact" value="mail">

If the group is within a <form> element: (intentionally did not break the for iterator)

for (const [index, input] of document.forms[0].contact.entries())
  if(input.value === `phone`)
    input.checked = true

<form>
  <input type="radio" name="contact" value="email">
  <input type="radio" name="contact" value="phone">
  <input type="radio" name="contact" value="mail">
</form>

Solution 12 - Jquery

$('input[name="mygroup"]').val([5])

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
QuestiondcolumbusView Question on Stackoverflow
Solution 1 - JqueryFelix KlingView Answer on Stackoverflow
Solution 2 - JqueryJonathan PasquierView Answer on Stackoverflow
Solution 3 - JqueryDavid ThomasView Answer on Stackoverflow
Solution 4 - JqueryhunterView Answer on Stackoverflow
Solution 5 - JqueryJean-Marc AmonView Answer on Stackoverflow
Solution 6 - JquerykaranView Answer on Stackoverflow
Solution 7 - JqueryJukkaVView Answer on Stackoverflow
Solution 8 - JqueryKetanView Answer on Stackoverflow
Solution 9 - JqueryDinushika RathnayakeView Answer on Stackoverflow
Solution 10 - JqueryAmin FathiView Answer on Stackoverflow
Solution 11 - JqueryvsyncView Answer on Stackoverflow
Solution 12 - JqueryAnjithaView Answer on Stackoverflow