obb-taurus
asked on
How do I determine which radio button is selected in a radiobutton list using javascript
Hi,
I'm quite new to javascript so I'm having alot of problems figuring this out. On my web form I am trying to figure out how to use Javascript to determine which radio button is selected from a radio button list. Once I know which radio list item is selected, I then want to display various panels based on the selection.
Please see attached code. I would like to have a javascript solution not jquery.
Thanks
I'm quite new to javascript so I'm having alot of problems figuring this out. On my web form I am trying to figure out how to use Javascript to determine which radio button is selected from a radio button list. Once I know which radio list item is selected, I then want to display various panels based on the selection.
Please see attached code. I would like to have a javascript solution not jquery.
Thanks
var selectedButton = getSelectedRadioButton("TypeSelection")
var getSelectedRadioButton = function (controlName) {
for (var i = 0; i <= controlName.lenght; i++)
if (controlName[i].checked) {
return controlName[i];
}
else
return "";
}
ASKER
I tried your code and get the following error.
Object doesn't support property or method 'getElementsByTagName'
I should have mentioned that the radio button list is in a content page based on a master page and the script is located in a external file. I have also registered the external file from the code behind as follows:
Page.ClientScript.Register ClientScri ptInclude( "clearform ", ResolveUrl(@"Scripts\JSScr ipt.js"));
btnClear.Attributes.Add("o nclick", "return clearInvoiceItemControls() ;");
I have attached to a button on the form for testing. I will be using this javascript function for clearing the form as well once I get this part figured out.
Not sure how much of a difference the above will make in your code sample.
Thanks
Object doesn't support property or method 'getElementsByTagName'
I should have mentioned that the radio button list is in a content page based on a master page and the script is located in a external file. I have also registered the external file from the code behind as follows:
Page.ClientScript.Register
btnClear.Attributes.Add("o
I have attached to a button on the form for testing. I will be using this javascript function for clearing the form as well once I get this part figured out.
Not sure how much of a difference the above will make in your code sample.
Thanks
RadioButtonList1 is the ID of your radiobutton list else replace it with your own ID
ASKER
When I tried your code the first time I altered the code to match my name so in my case it would be:
transactionTypeSelection.C lientID and this generated the error.
transactionTypeSelection.C
my bad, we need to add [0] :
var rbs = document.getElementsByName ("<%= transactionTypeSelection.C lientID %>")[0].getElementsByTagName("inp ut");
var rbs = document.getElementsByName
Correction :
var rbs = document.getElementById("< %= transactionTypeSelection.C lientID %>").getElementsByTagName( "input");
var rbs = document.getElementById("<
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so much for the awesome help.
Open in new window