473,545 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="javas cript">
function checkInput(sent CheckedStatus)
{
alert("The radio button status is " + sentCheckedStat us);
}
</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="RBserve r.php" method="post"
onsubmit="check Input(theButton s.value)"> <br>
/* Substitute ...(theButtons. checked)... in the line above and it
still returns "undefined" no matter what. */

<br>
<center> <input name="theButton s" value="it" type="radio">IT <br>
<input name="theButton s" value="notit" type="radio">NO T 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 1436
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="javas cript">
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
9804
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 update record page that reads information in the database, and presently it will not retrieve the previously selected option from the database. I have...
3
3579
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. I need to be able to send to a stored procedure the areas that the client has selected so these can be stored in the database.
4
3261
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> <input type="radio" name=p2 value=2> <input type="radio" name=p2 value=3> then a text area and a button:
4
11024
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 to: if one is clicked, its corresponding information will become available on another document, if it's not clicked no information will be...
3
2400
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 get the group names selected value, I couldn't find any properties/methods on that object. In fact the object didn't exist for me. Is this...
22
7928
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 am using getElelementsByName since these are radio buttons, but it seems that the dynamic element is not seen!!! This is my code... please let...
1
6855
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 grandtotal the costs ready for the selected items to be inserted back to the database. I did something like this before with Checkboxes, but...
2
5881
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 the form by changing the visible state of the radio buttons and labels utilising back and next buttons. E.g. Next button makes current radio...
7
3617
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 the form again (if the respondent didn't finish the whole form the first time). My text and textarea fields are inserting just fine and I'm able to...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.