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

returning values from radio buttons

Status
Not open for further replies.

goobydooby

Technical User
Apr 22, 2001
1
GB
I am a novice javascript programmer.

I am trying to use radio buttons to make a choice between yes and no on a form. I then want to display the value of the checked button.

I have looked in the archives and come up with the following but in IE5 it will not display the value. Any tips or have I missed something obvious.
Many thanks in advance.

<P>

<B> Are there sensitive neighbours nearby? </B>

<!-- sets constraints for noise and odour with radio buttons to choose yes or no-->


Yes: <INPUT TYPE = &quot;RADIO&quot; NAME = &quot;Neighbours&quot; VALUE=&quot;true&quot; checked>

No: <INPUT TYPE = &quot;RADIO&quot; NAME = &quot;Neighbours&quot; VALUE=&quot;false&quot;>

</FORM>
<P>

<SCRIPT>
//script to use value from radio buttons to determine
//the value of the variable neighbours
//as there is only a choice between two buttons a
//simple if else statement is used

var neighbours=&quot;&quot;;

IF(document.Decision.Neighbours[1].checked)

{neighbours=&quot;false&quot;;}

ELSE{neighbours=&quot;true&quot;};

document.write(neighbours)

</SCRIPT>



 
I get an error when I try to use capitals for IF/ELSE. I think this code is writing &quot;&quot; - an empty string, because that's what is stored in the variable - The code you have there should be called in a FUNCTION because the way it is, it is executed the first time it is read - and the user has no chance to even press a button. aving sadi that, by rights it should have written &quot;true&quot; to the page...
seriously - try using if and else. ;-)
b2 - benbiddington@surf4nix.com
 
Yeah, it needs to be in a function. I've just worked this out on my own and that script is fine inside a function.

Also not that the VALUE attribute is unnecessary as the script accesses radio[1], the second button in the array and does not use the true/false values in VALUE.

I say this because if you try to use more than 2 buttons this script will not work by accessing the VALUE, a mistake I made and wasted 1/2 a day on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top