473,326 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

radio buttons and their values

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
2 1429
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: entoone | last post by:
I am able to make a selection of information when entering a record, with radio buttons giving the option of yes, or no. Which stores their answer as yes, or no in the database. I then have an...
3
by: Suzanne | last post by:
Hi, I have a form which our clients can fill in with their personal details. As part of the information we store there is a section - areas of interest - this is a number of radio buttons. ...
4
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
4
by: Jared | last post by:
Radio Button or Check Box and Event Procedures I need to insert either radio buttons or check boxes onto my form. I'm not sure which to use, or if there are other options. I am using the buttons...
3
by: Flip | last post by:
In setting up a radio button group last night, I added two radio buttons, gave each of them their respective name and gave them both the same group name. So far so good. But then when I tried to...
22
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I...
1
by: kenny8787 | last post by:
Hi, can anyone help here? I have the following code generated from a database, I want to have javascript calculate the costs of the selected items using radio buttons, subtotal the costs and...
2
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change between which set of questions is currently shown on...
7
by: help4me | last post by:
Hope someone can help me out with this. I'm having trouble getting my radio button values into a MySQL database. And after I do get them inserted I'm going to want to be able to show them back in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.