If the radio button is the srcElement, you can just use that.
Otherwise, if you know the name of the radio button, you can access the entire group of radio buttons via document.formName.radioButtonName. This will yield an array that you can loop through to find the one with .selected=true, and then you can take the
.value attribute from that one.
Does that help?
Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com Do the impossible, and go home early.
No; document.getElementById will only get the element with the ID you specify (the HTML spec is quite clear that only one element on a page can have a specific ID).
Each radio button has a different ID attribute, but if you look at the HTML source of a page, you will see that all radio buttons in the list have the same NAME attribute. This is what I meant by "the name of the radio button".
Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com Do the impossible, and go home early.
Well i dont know the id of each and every item in the radio button list and i cant write for each because they could be any number of item in a radio button list.
i was just wondering if i could get the value just like i get the text of the selected item by a single line , if not any sample you have which can help
I have the following code on in my page but it doesnt seems to work.
var radioButtons=document.getElementsByName('<%=rblCustomer.ClientID%>');
for (var x = 0; x < radioButtons.length; x ++)
{
alert(radioButtons[x].id);
if (radioButtons[x].checked)
{
alert("You checked " + radioButtons[x].id);
}
}
The length is always "1" and i have 4 items in the radio button list. Plus radioButtons[x].id throws an No object or null error.
ClientID is the ID of the HTML element. The NAME attribute is probably something else (do a View Source and look at the HTML to see the difference). You can probably use the control's UniqueID attribute to get the client-side name, but I could be wrong.
If that doesn't work, try this:
var oneRadioButton = document.getElementById("<%=control.ClientID%>");
var rblName = oneRadioButton.name;
var radioButtons = document.getElementsByName(rblName);
// Then loop through the array as you've done above
Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com Do the impossible, and go home early.
ankit1407
Member
63 Points
140 Posts
Radio Button List Selected Value in JavaScript
Jul 19, 2007 05:17 PM|LINK
Hi
I need to get the selected value of te radio button list in the java script . I am using the following line of code
var chkText = event.srcElement.parentElement.lastChild.innerHTML;
it gives me the text of the selected item not the value .
any idea ?
PeterBrunone
All-Star
18495 Points
3683 Posts
MVP
Re: Radio Button List Selected Value in JavaScript
Jul 19, 2007 05:28 PM|LINK
If the radio button is the srcElement, you can just use that.
Otherwise, if you know the name of the radio button, you can access the entire group of radio buttons via document.formName.radioButtonName. This will yield an array that you can loop through to find the one with .selected=true, and then you can take the .value attribute from that one.
Does that help?
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
ankit1407
Member
63 Points
140 Posts
Re: Radio Button List Selected Value in JavaScript
Jul 19, 2007 05:41 PM|LINK
thanks for the reply i know the name of the radio button list.
I tried using this to find the object .
obj=document.getElementById('<%=rblCustomer.ClientID%>');
Is that what you mean by returning an array
PeterBrunone
All-Star
18495 Points
3683 Posts
MVP
Re: Radio Button List Selected Value in JavaScript
Jul 19, 2007 05:57 PM|LINK
No; document.getElementById will only get the element with the ID you specify (the HTML spec is quite clear that only one element on a page can have a specific ID).
Each radio button has a different ID attribute, but if you look at the HTML source of a page, you will see that all radio buttons in the list have the same NAME attribute. This is what I meant by "the name of the radio button".
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
ankit1407
Member
63 Points
140 Posts
Re: Radio Button List Selected Value in JavaScript
Jul 19, 2007 06:03 PM|LINK
Well i dont know the id of each and every item in the radio button list and i cant write for each because they could be any number of item in a radio button list.
i was just wondering if i could get the value just like i get the text of the selected item by a single line , if not any sample you have which can help
thanks
bpw
Contributor
2258 Points
490 Posts
Re: Radio Button List Selected Value in JavaScript
Jul 19, 2007 06:55 PM|LINK
GetElementsByName will return all the radio buttons in the group. For example, if your radio buttons are named ‘test’:
1 function getCheckedRadio() {
2 var radioButtons = document.getElementsByName("test");
3 for (var x = 0; x < radioButtons.length; x ++) {
4 if (radioButtons[x].checked) {
5 alert("You checked " + radioButtons[x].id);
6 }
7 }
8 }
bpw
Contributor
2258 Points
490 Posts
Re: Radio Button List Selected Value in JavaScript
Jul 19, 2007 07:01 PM|LINK
Oh, to get the value:
alert("You checked " + radioButtons[x].id + " which has the value " + radioButtons[x].value);
ankit1407
Member
63 Points
140 Posts
Re: Radio Button List Selected Value in JavaScript
Jul 19, 2007 08:14 PM|LINK
Hi thanks for the suggestion.
I have the following code on in my page but it doesnt seems to work.
var radioButtons=document.getElementsByName('<%=rblCustomer.ClientID%>');
for (var x = 0; x < radioButtons.length; x ++)
{
alert(radioButtons[x].id);
if (radioButtons[x].checked)
{
alert("You checked " + radioButtons[x].id);
}
}
The length is always "1" and i have 4 items in the radio button list. Plus radioButtons[x].id throws an No object or null error.
any suggestions
PeterBrunone
All-Star
18495 Points
3683 Posts
MVP
Re: Radio Button List Selected Value in JavaScript
Jul 19, 2007 08:29 PM|LINK
ClientID is the ID of the HTML element. The NAME attribute is probably something else (do a View Source and look at the HTML to see the difference). You can probably use the control's UniqueID attribute to get the client-side name, but I could be wrong. If that doesn't work, try this:
var oneRadioButton = document.getElementById("<%=control.ClientID%>");
var rblName = oneRadioButton.name;
var radioButtons = document.getElementsByName(rblName);
// Then loop through the array as you've done above
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
bpw
Contributor
2258 Points
490 Posts
Re: Radio Button List Selected Value in JavaScript
Jul 19, 2007 08:34 PM|LINK
This statement isn't right:
var radioButtons=document.getElementsByName('<%=rblCustomer.ClientID%>');
It has to have the ‘name’ of the radio button group not the ‘id’ of an individual button. For example, if you have a group of ASP buttons:
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="test" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="test" />
<asp:RadioButton ID="RadioButton3" runat="server" GroupName="test" />
or HTML buttons
<input id="Radio1" type="radio" name="test" value="1" />
then you would use:<input id="Radio2" type="radio" name="test" value="2" />
<input id="Radio3" type="radio" name="test" value="3" />
var
radioButtons = document.getElementsByName("test");