By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,623 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,623 IT Pros & Developers. It's quick & easy.

radio buttons and their values

P: n/a
I've seen several messages in this group with this question and no reply.
Simply stated, how do you send the status of a group of radio buttons to
a JavaScript function? I have a large project that needs this. Since I
cannot figure it out, I wrote a small page to experiment with it. Here
it is.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>An Experiment In Radio Buttons</title>
<script language="javascript">
function checkInput(sentCheckedStatus)
{
alert("The radio button status is " + sentCheckedStatus);
}
</script>
</head>
<body style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);"
alink="#000088" link="#0000ff" vlink="#ff0000">
This is an experiment in radio buttons.
<form name="theForm" action="RBserver.php" method="post"
onsubmit="checkInput(theButtons.value)"> <br>
/* Substitute ...(theButtons.checked)... in the line above and it
still returns "undefined" no matter what. */

<br>
<center> <input name="theButtons" value="it" type="radio">IT<br>
<input name="theButtons" value="notit" type="radio">NOT IT<br>
<br>
<input value="give it a go" type="submit"> </center>
</form>
</body>
</html>

I know I can send the value of a text box or another input field this
way. Thank you in advance for your help.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
It doesn't work because if you have more than one radio button with the
same name, the radio object becomes an array of radio objects. An
array doesn't have a .checked property; you would have to do something
like theButtons[0].checked.

If you want a function to return the value of the checked radio button,
try this link:

Javascript Get or Set Checked Radio Value
http://www.somacon.com/blog/page43.php

Jul 23 '05 #2

P: n/a
Jeff Sandler wrote:
I've seen several messages in this group with this question and no reply.
Simply stated, how do you send the status of a group of radio buttons to
a JavaScript function?
Not quite sure what you mean here. A group of radio buttons should
always have one selected. Most user agents don't enforce it however,
so you should make one selected in the HTML and JavaScript is not
required at all.
I have a large project that needs this. Since I
cannot figure it out, I wrote a small page to experiment with it. Here
it is.

Here is a new "checkInput" function that checks to see which button
is selected:

<script type="text/javascript">
function checkInput(x) {
var i = x.length;
while (i--) {
if (x[i].checked) {
alert('You selected ' + x[i].value);
return true;
}
}
alert("Please check a button");
return false;
}
</script>

And here is the onsubmit call:

<form name="theForm" action="" onsubmit="
return checkInput(this.theButtons);
">
Note that if no button is selected, the function returns false and
the form is not submitted. It is not necessary to include "true"
in the "return true" line, but I like it 'cos it's explicit that the
function returns true.

i.e. you could just have:

if (x[i].checked) {
alert('You selected ' + x[i].value);
return;
}
[...] <script language="javascript">
Language is depreciated, use "type" as above.

[...]
I know I can send the value of a text box or another input field this
way. Thank you in advance for your help.


Any control that is successful will be sent. If one button in a set
of radios is selected, the control is successful and its value will be
sent - no need for JavaScript at all.

And remember to validate back on the server. Client-side validation
is no guarantee at all, it just saves the user a round-trip if the
form is not valid.
--
Rob
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.