[Javascript] Javascript - Form value undefined
David Lovering
dlovering at gazos.com
Tue Nov 11 08:21:16 CST 2003
While this is all perfectly correct, it is sometimes easier to combine the
two routines together, and return the value after the first successful 'hit'
within the radio-button group. In other words:
function radio_active(radio_group) {
// Run through the group
for (counter = 0; counter < radio_group.length; counter++) {
// When we find the activated button, return the index
if (radio_group[counter].checked) {
return radio_group[counter].value;
}
}
// If no button is activated, return -1
return -1
}
This runs a LOT faster, particularly if your web page gets a lot of hits
and/or you have a humongous number of radio buttons in the group.
-- Dave Lovering
----- Original Message -----
From: "Tim Makins" <spindrift at oceanfree.net>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Tuesday, November 11, 2003 12:21 AM
Subject: Re: [Javascript] Javascript - Form value undefined
> ----- Original Message -----
> From: "Tim Burgan" <lists.latech.edu at greaterthani.com>
> To: "[JavaScript List]" <javascript at LaTech.edu>
> Sent: Tuesday, November 11, 2003 7:04 AM
> Subject: [Javascript] Javascript - Form value undefined
>
>
> > <http://www.greaterthani.com/05_programming/week5help.htm>
> >
> > The form with the radio button displays a result of 'undefined', when it
> > should be either 0 or 0.25
> >
> > Can someone please take a look at this to kelp me out?
>
>
> Always happy to kelp out if I can !
>
> You can't read radio values like that. You must see if they are checked or
> not first.
> Look at the script below (not mine)
>
> Tim in Ireland.
>
> <html>
> <head>
> <title>Listing 28.7. Finding the Activated Radio Button In a Group</title>
>
> <script language="JavaScript" type="text/javascript">
> <!--
>
> function radio_active(radio_group) {
>
> // Run through the group
> for (counter = 0; counter < radio_group.length; counter++) {
>
> // When we find the activated button, return the index
> if (radio_group[counter].checked) {
> return counter
> }
> }
> // If no button is activated, return -1
> return -1
> }
>
> function display_radio_value(current_form) {
>
> // Get the index of the activated button
> var radio_index = radio_active(current_form.freight_group)
>
> // If a button is activated, display its value in the text box
> if (radio_index >= 0) {
> current_form.selected_radio.value =
> current_form.freight_group[radio_index].value
> }
> }
>
> //-->
> </script>
>
> </head>
>
> <body>
>
> <form>
>
> <input type="radio" name="freight_group" value="FedEx" checked>Federal
> Express
> <br>
> <input type="radio" name="freight_group" value="UPS">United Parcel Service
> <br>
> <input type="radio" name="freight_group" value="Airborne">Airborne Express
> <br>
> <input type="radio" name="freight_group" value="Mail">Surface Mail
> <br>
> <input type="text" name="selected_radio">
> <br>
> <input type="button" value="Display Active Radio Value"
> onClick="display_radio_value(this.form)">
>
> </form>
>
> </body>
> </html>
>
>
>
>
>
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
More information about the Javascript
mailing list