Reputation: 169
I just want to ask how can I select the value of my radiobuttonlist here is my code.
function CheckRadioButton(rdbRemarks){
document.getElementByID(rdbRemarks).SelectedValue = "1";
}
Here is the code of my button onclick
<asp:Button ID="CheckRadioButton" CssClass="ButtonCalibri" Width="80px" runat="server" Text="Save" onclick="CheckRadioButton('<%=rdbRemarks.ClientID%>')" />
Here is the code of my radiobuttonlist in case you need a reference
<asp:RadioButtonList ID="rdbRemarks" CssClass="LabelCalibri" runat="server" onselectedindexchanged="rdbRemarks_SelectedIndexChanged">
<asp:ListItem Value="1" Text="Charge to Utilization"></asp:ListItem>
<asp:ListItem Value="2" Text="Charge to Operating Expense"></asp:ListItem>
</asp:RadioButtonList>
What I want is when I click the button, one of the radio button in the list will be selected, for example the selected value is "1" the first radio button will be selected. I currently have no idea how to do that now, Please help me on this one.
Upvotes: 1
Views: 37883
Reputation: 170
Get RadioButtonList Selected value using jQuery
var selectedVal=$("[id$='RadioButtonList1']").find(":checked").val();
Upvotes: 5
Reputation: 21
function Validation(){
var radioButtonlist=document.getElementById(<%=rdbdeductionList.ClientID%>");
var inputs = radioButtonlist.getElementsByTagName('input');
var flag = false;
var selected;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
selected = inputs[i];
flag = true;
break;
}
}
if (flag) {
alert(selected.value);
}
else {
alert('Please select an option.!');
return false;
}
}
Upvotes: 2
Reputation: 2215
The selected value can be retrieved using JavaScript with the line below:
var selectedvalue = $('#<%= yourRadioButtonList.ClientID %> input:checked').val()
Upvotes: 0
Reputation: 21931
In your button click
event
Inside your script
document.getElementById("idofradiobutton").checked = true;
JQuery solution:
$("#idofradiobutton").prop("checked", true);
//....TAKE THE SELECTED VALUE OF RADIO BUTTON
var radiovalue= document.getElementById('idofradiobutton').value;
if (radiovalue=="1") {
document.getElementById('idofcorespondingradiobutton').checked = true;
}
//if you are using radiobutton list then try this
function getCheckedRadio() {
var radioButtons = document.getElementsByName("IDofRadiobuttonlist");
for (var x = 0; x < radioButtons.length; x ++) {
if (radioButtons[x].checked) {
alert("You checked " + radioButtons[x].id + " which has the value " + radioButtons[x].value);
}
}
}
Upvotes: 4
Reputation: 3495
Try This
var elementRef = document.getElementById("rdbRemarks"); // ID of DropDown
for (var i=0; i<elementRef.options.length; i++)
{
if ( elementRef.options[i].value == "1") // value you want to select
{
elementRef.options[i].selected = true;
}
}
OR (JQuery)
var value = 1;
$("input[id=rdbRemarks][value=" + value + "]").attr('checked', 'checked');
//OR
$("input[id=mygroup][value=" + value + "]").prop('checked', true);
Upvotes: 0