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=myg
Pure JavaScript version:
document.querySelector('input[name="myradio"][value="5"]').checked = true;
$('input[name="mygroup"]').val([5])
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);
}
})
}
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');
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');
$("input[name='mygroup'][value='5']").attr("checked", true);