Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referring to a .net radio button list using javascript

Status
Not open for further replies.

MaffewW

Technical User
Apr 9, 2002
438
GB
Hi

I'm calling a javascript function from an asp.net page. The function pops a confirmation box with some information:

Code:
btnContinue.Attributes.Add("onClick", "score();")

Code:
function score(){
//Calculate the score 
	var points = 0;
			
        if(document.getElementById("rblFullNameGiven").Value == "Yes"){
								points = points + 1;
			}
				
return window.confirm('Points: ' + points);
			return;
		}

I have the following .net control:

Code:
<asp:radiobuttonlist id="rblFullNameGiven" runat="server" Width="88px" RepeatDirection="Horizontal">
							<asp:ListItem Value="Yes">Yes</asp:ListItem>
							<asp:ListItem Value="No">No</asp:ListItem>
						</asp:radiobuttonlist>

For some reason, even when I select "Yes" on the radio button list and hit my continue button, the code in my if statement doesn't get executed - hence leaving my 'points' variable with a value of 0. If however I change it to != "Yes", it works. Can anyone see what's wrong with the syntax?!

Thanks very much in advance.



Matt

Brighton, UK
 
Further to the above, I've found out that asp renames the control when the page is rendered:

Code:
TD style="WIDTH: 170px; HEIGHT: 38px"><table id="rblFullNameGiven" border="0" style="width:88px;">
	<tr>
		<td><input id="rblFullNameGiven_0" type="radio" name="rblFullNameGiven" value="Yes" /><label for="rblFullNameGiven_0">Yes</label></td><td><input id="rblFullNameGiven_1" type="radio" name="rblFullNameGiven" value="No" /><label for="rblFullNameGiven_1">No</label></td>
	</tr>

I then added these lines of code as a test:

Code:
alert(document.Form1.rblUKResident.value)
			alert(document.Form1.rblUKResident_0.value)
			alert(document.Form1.rblUKResident_1.value)

This produces these three values:

undefined
Yes
No

..where the radio button list has been selected or not. Can anyone tell me how to reference the actual selected value, i.e. if it's a "Yes" return yes and if it's a "No" return no.

Any help would be really appreciated.

Thanks a lot.



Matt

Brighton, UK
 
Refer to the radio buttons by name instead of id. Try this:
Code:
//Calculate the score 
function score(){
  var points = 0;

  if(getRadioValue("rblFullNameGiven") == "Yes")
    points = points + 1;

  return window.confirm('Points: ' + points);
}

function getRadioValue(name){
  var radios = document.getElementsByName(name);

  for(var i=0; i<radios.length; i++){
    if(radios[i].checked) return radios[i].value;
  }
}

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top