How to set radio button selected value in javascript:
HTML Code :
<input type="radio" name="RBLExperienceApplicable" class="radio" value="1" >
<input type="radio" name="RBLExperienceApplicable" class="radio" value="0" >
<input type="radio" name="RBLExperienceApplicable2" class="radio" value="1" >
<input type="radio" name="RBLExperienceApplicable2" class="radio" value="0" >
ASPX Code
<asp:RadioButtonList ID="RBLExperienceApplicable" runat="server" class="radio" RepeatDirection="Horizontal" EnableViewState="false">
<asp:ListItem Value="1" Text="Yes "></asp:ListItem>
<asp:ListItem Value="0" Text="No"></asp:ListItem>
</asp:RadioButtonList>
<asp:RadioButtonList ID="RBLExperienceApplicable2" runat="server" class="radio" RepeatDirection="Horizontal" EnableViewState="false">
<asp:ListItem Value="1" Text="Yes "></asp:ListItem>
<asp:ListItem Value="0" Text="No"></asp:ListItem>
</asp:RadioButtonList>
// Some Code Fetch from db
// Based on db value script block will execute
<script type="text/javascript">
RadionButtonSelectedValueSet('1');
</script>
Script Block :
function RadionButtonSelectedValueSet(SelectdValue) {
$('#RBLExperienceApplicable').val(SelectdValue);
//$("input[name='RBLExperienceApplicable']:checked").val(SelectdValue);
}
This question is related to
javascript
jquery
asp.net
.net
This is the only syntax that worked for me
$('input[name="assReq"][value="' + obj["AssociationReq"] + '"]').prop('checked', 'checked');
A clean approach I find is by giving an ID
for each radio button and then setting it by using the following statement:
document.getElementById('publicedit').checked = true;
<input type="radio" name="RBLExperienceApplicable" class="radio" value="1" checked >
// For Example it is checked
<input type="radio" name="RBLExperienceApplicable" class="radio" value="0" >
<input type="radio" name="RBLExperienceApplicable2" class="radio" value="1" >
<input type="radio" name="RBLExperienceApplicable2" class="radio" value="0" >
$( "input[type='radio']" ).change(function() //on change radio buttons
{
alert('Test');
if($('input[name=RBLExperienceApplicable]:checked').val() != '') //Testing value
{
$('input[name=RBLExperienceApplicable]:checked').val('Your value Without Quotes');
}
});
If the selection changes, make sure to clear the other checks first. al la...
$('input[name="' + value.id + '"]').attr('checked', false);_x000D_
$('input[name="' + value.id + '"][value="' + thing[value.prop] + '"]').attr('checked', true);_x000D_
_x000D_
You can try this also
function SetRadiobuttonValue(selectedValue)
{
$(':radio[value="' + selectedValue + '"]').attr('checked', 'checked');
}
$('input[name="RBLExperienceApplicable"]').prop('checked',true);
Thanks dude...
for multiple dropdowns
$('[id^=RBLExperienceApplicable][value='+ SelectedVAlue +']').attr("checked","checked");
here RBLExperienceApplicable
is the matching part of the radio button groups input tag ids.
and [id^=RBLExperienceApplicable]
matches all the radio button whose id start with RBLExperienceApplicable
Can be done using the id of the element
example
<label><input type="radio" name="travel_mode" value="Flight" id="Flight"> Flight </label>
<label><input type="radio" name="travel_mode" value="Train" id="Train"> Train </label>
<label><input type="radio" name="travel_mode" value="Bus" id="Bus"> Bus </label>
<label><input type="radio" name="travel_mode" value="Road" id="Road"> Other </label>
js:
$('#' + selected).prop('checked',true);
Below script works fine in all browser:
function RadionButtonSelectedValueSet(name, SelectdValue) {
$('input[name="' + name + '"][value="' + SelectdValue + '"]').attr('checked',true);
}
<asp:RadioButtonList ID="rblRequestType">
<asp:ListItem Selected="True" Value="value1">Value1</asp:ListItem>
<asp:ListItem Value="Value2">Value2</asp:ListItem>
</asp:RadioButtonList>
You can set checked like this.
var radio0 = $("#rblRequestType_0");
var radio1 = $("#rblRequestType_1");
radio0.checked = true;
radio1.checked = true;
You can do more elegantly.
function RadionButtonSelectedValueSet(name, SelectedValue) {
$('input[name="' + name+ '"]').val([SelectedValue]);
}
document.getElementById("TestToggleRadioButtonList").rows[0].cells[0].childNodes[0].checked = true;
where TestToggleRadioButtonList
is the id of the RadioButtonList
.
Source: Stackoverflow.com