PDA

View Full Version : Radio button - Value



roninden
Nov 29th, 2002, 11:46 PM
I trying to retrieve a value from a my radio button in a form but I get either a NaN or undefined. what am I doing wrong? Here my code:

<html>
<head>

<script>

function getradio ()

{
example.answer.value=parseInt(example.getit.value)

}
</script>
<head>
<body>
<form name="example">
<input type="radio" name="getit" value="0" checked="checked">without<br/ >
<input type="radio" name="getit" value="5.98">with for $5.98<br/ >
<input type="text" name="answer">
<input type="button" onClick="getradio ()" value="get more">
</form></body>
</html>

Graeme Hackston
Nov 30th, 2002, 05:04 AM
You have 3 things wrong.

1/ Names have to be different

2/ ParseFloat changes a string to a number.

3/ you have to use document when refferencing a form element.

<html>
<head>

<script>

function getradio ()

{
document.example.answer.value=parseFloat(document.example.getit2.value)

}
</script>
</head>
<body>
<form name="example">
<input type="radio" name="getit" value="0" checked="checked">without<br/ >
<input type="radio" name="getit2" value="5.98">with for $5.98<br/ >
<input type="text" name="answer">
<input type="button" onClick="getradio ()" value="get more">
</form></body>
</html>

Graeme Hackston
Nov 30th, 2002, 05:08 AM
Also <br/ > should be <br />

chrismiceli
Nov 30th, 2002, 05:28 AM
the way you have it gene they can select multiple radios, like checkboxes, i don't think he wants that, he needs it to be an either or. i don't you radiobuttons, but you need to find out which one is selected and check its value like this


<html>
<head>
<script type="text/javascript">
function getradio() {
document.example.answer.value=parseFloat(document.example.getit[the selected one].value) ;
}
</script>
</head>
<body>
<form name="example">
<input type="radio" name="getit" value="0" checked="checked">without<br/ >
<input type="radio" name="getit2" value="5.98">with for $5.98<br/ >
<input type="text" name="answer">
<input type="button" onClick="getradio()" value="get more">
</form></body>
</html>

i don't know how to see which one is selected though, it is like in select boxes document.forms[0].selectname.options[document.forms[0].selectname.selectedIndex].value.

roninden
Nov 30th, 2002, 06:23 AM
Thanks for the suggestion but giving the radio button different names turn them into check boxes. I don't want to be able to select both button just either/or. Neither suggest works for me

Graeme Hackston
Nov 30th, 2002, 07:12 AM
You're right, radios have the same name. I've never used them.

Is this what you need?

<html>
<head>

<script>

function getradio (value)

{
document.example.answer.value=parseFloat(value)

}
</script>
</head>
<body>
<form name="example">
<input type="radio" name="getit" value="0" onclick="getradio (this.value)" checked="checked">without<br/ >
<input type="radio" name="getit" value="5.98" onclick="getradio (this.value)">with for $5.98<br/ >
<input type="text" name="answer">
<input type="button" onClick="getradio ()" value="get more">
</form></body>
</html>

roninden
Nov 30th, 2002, 05:12 PM
It came to me last night when I was trying to sleep. Here the solution that works

<html>
<head>

<script>
var temp=0
function setOff()
{
temp=0
}
function setOn()
{
temp=5.98
}
function getradio (getit)

{
document.example.answer.value=temp

}
</script>
</head>
<body>
<form name="example">
<input type="radio" name="getit" value="0" checked="checked" onFocus="setOff()">without<br/ >
<input type="radio" name="getit" value="5.98" onFocus="setOn()">with for $5.98<br/>
<input type="text" name="answer">
<input type="button" onClick="getradio ()" value="get more">
</form></body>
</html>

glenngv
Dec 2nd, 2002, 10:35 AM
<html>
<head>
<script type="text/javascript">
function getradio (f)
{
f.answer.value=(f.getit[0].checked)?f.getit[0].value:f.getit[1].value;
}
</script>
</head>
<body>
<form name="example">
<input type="radio" name="getit" value="0" checked="checked">without<br />
<input type="radio" name="getit" value="5.98" onclick="getradio (this.value)" />with for $5.98<br />
<input type="text" name="answer" value="0" />
<input type="button" onClick="getradio(this.form)" value="get more" />
</form>
</body>
</html>

tonyeveland
Dec 3rd, 2002, 05:30 PM
Glenn,

This is good:
<html>
<head>
<script type="text/javascript">
function getradio (f)
{
f.answer.value=(f.getit[0].checked)?f.getit[0].value:f.getit[1].value;
}
</script>
</head>
<body>
<form name="example">
<input type="radio" name="getit" value="0" checked="checked">without<br />
<input type="radio" name="getit" value="5.98" onclick="getradio (this.value)" />with for $5.98<br />
<input type="text" name="answer" value="0" />
<input type="button" onClick="getradio(this.form)" value="get more" />
</form>
</body>
</html>

but what do you do with, say, 5 buttons in the "getit" button group?

Tony
__________________

tonyeveland
Dec 4th, 2002, 12:41 AM
Thanks again