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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting selected item from RadioButtonList

Status
Not open for further replies.

iaswnidou

Programmer
Joined
Apr 19, 2005
Messages
140
Location
GR
Hello

I have a radiobuttonlist on a asp.net page and i want to retrieve the value of the selected item.

Here is the HTML returned:
Code:
<table id="rdbTypeList" onmouseup="ToggleFlatsMaisonette()" border="0">
	<tr>
		<td><input id="rdbTypeList_0" type="radio" name="rdbTypeList" value="1" /><label for="rdbTypeList_0">House</label></td><td><input id="rdbTypeList_1" type="radio" name="rdbTypeList" value="2" /><label for="rdbTypeList_1">Flat / Studio</label></td><td><input id="rdbTypeList_2" type="radio" name="rdbTypeList" value="3" /><label for="rdbTypeList_2">Maisonette</label></td><td><input id="rdbTypeList_3" type="radio" name="rdbTypeList" value="4" /><label for="rdbTypeList_3">Bungalow</label></td><td><input id="rdbTypeList_4" type="radio" name="rdbTypeList" value="5" /><label for="rdbTypeList_4">Bedspace</label></td>
	</tr>
	<tr>
		<td><input id="rdbTypeList_5" type="radio" name="rdbTypeList" value="6" /><label for="rdbTypeList_5">Live Work Unit</label></td><td><input id="rdbTypeList_6" type="radio" name="rdbTypeList" value="7" /><label for="rdbTypeList_6">Static Home</label></td><td><input id="rdbTypeList_7" type="radio" name="rdbTypeList" value="8" /><label for="rdbTypeList_7">Garage</label></td><td><input id="rdbTypeList_8" type="radio" name="rdbTypeList" value="9" /><label for="rdbTypeList_8">Parking space</label></td>
	</tr>
</table>

Can you do this in 1 line of code?
 
When i do this:

Code:
var radio = document.forms[0].elements['rdbTypeList'];
alert(radio.value);

the alert produces "undefined".
 
Can you do this in 1 line of code?
Sure you can - although it may not be very easy to read... but you can do it.

Are you wanting to do this in the ToggleFlatsMaisonette() function? You would loop through all the radios and test each one... since only one radio (of a group) can be selected.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Code:
var radio = document.forms[0].elements['rdbTypeList'];

if (radio[1].checked || radio[2].checked)
{ //bla bla }
else
{ //bla bla }

This way produces bizarre results..do i have a syntax error?
 
I call the function like this "onClick="ToggleFlatsMaisonette()", which contains:

Code:
function ToggleFlatsMaisonette()
{
  var radio = document.forms[0].elements['rdbTypeList'];
	
  if (radio[1].checked)
  {
    document.forms[0].elements['rdbStoreyList'].disabled=   false;
  }
  else
  {
    document.forms[0].elements['rdbStoreyList'].disabled = true;
  }
}

This code does not work however.
 
It has mysteriously worked...thanks for the help.
 
Well, I have created a stand-alone test page showing that it works fine for me in Firefox for Windows - when there is only one radio button called rdbStoreyList:
Code:
<html>
<head>
<script type="text/javascript">
function ToggleFlatsMaisonette()
{
  var radio = document.forms[0].elements['rdbTypeList'];

  if (radio[1].checked)
  {
    document.forms[0].elements['rdbStoreyList'].disabled=false;
  }
  else
  {
    document.forms[0].elements['rdbStoreyList'].disabled=true;
  }
}
</script>
</head>
<body>
<form action="">
<input type="radio" name="rdbTypeList" value="1">1<br/>
<input type="radio" name="rdbTypeList" value="2">2<br/>
<input type="radio" name="rdbTypeList" value="3">3<br/>
<hr>
<input type="radio" name="rdbStoreyList" value="4">4<br/>
</form>
<a href="javascript:ToggleFlatsMaisonette();">Test<a>
</body>
</html>
If you add in other radio buttons in the same rdbStoreyList group, then the code will error - because you need to disable each radio in the group. This is probably where it's all breaking for you. Try this eaxmple:
Code:
<html>
<head>
<script type="text/javascript">
function ToggleFlatsMaisonette()
{
  var radio = document.forms[0].elements['rdbTypeList'];
  var rdbStoreyListRadios = document.forms[0].elements['rdbStoreyList'];
  if (radio[1].checked)
  {
    for (var loop=0; loop<rdbStoreyListRadios.length; loop++)
      rdbStoreyListRadios[loop].disabled=false;
  }
  else
  {
    for (var loop=0; loop<rdbStoreyListRadios.length; loop++)
      rdbStoreyListRadios[loop].disabled=true;
  }
}
</script>
</head>
<body>
<form action="">
<input type="radio" name="rdbTypeList" value="1">1<br/>
<input type="radio" name="rdbTypeList" value="2">2<br/>
<input type="radio" name="rdbTypeList" value="3">3<br/>
<hr>
<input type="radio" name="rdbStoreyList" value="4">4<br/>
<input type="radio" name="rdbStoreyList" value="5">5<br/>
</form>
<a href="javascript:ToggleFlatsMaisonette();">Test<a>
</body>
</html>

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks Jeff

I didn't realize that you posted another comment on the other thread yesterday.
It works fine with iterating and disabling each radiobutton individually.

What i dont't understand is that to enable it now i have to iterate again and enable each one of them.
Yesterday i was disabling and enabling the radiobuttonlist without iterating. But now once i iterate and disable, i have to iterate again to enable.
 
Sorry..i meant to write the comment above to the other thread!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top