Solved

Getting an undifined from input with a type=radio

Posted on 2004-08-17
3
318 Views
Last Modified: 2008-01-09
I am trying to validate that all questions are answered before I submit a page.  When I try and pull the value of the <input type="radio" id="touring" name="touring" value=1> and I have more then one Radio button I keep getting undefind and no value.  If i have only one radio button then i can get the correct value.
Javascript

function submitUpdateProduction()
      {
            str = document.frmLAStageTixRegistration.touring.value
            if (str.length < 1)
                  {
                        alert("Please answer if this is a touring production or not. " + str);
                        document.frmLAStageTixRegistration.touring.focus()
                        return false;
                  }
            document.frmLAStageTixRegistration.action = "../lastage/registration.asp?updateproduction=1";
            document.frmLAStageTixRegistration.method = "post";
            document.frmLAStageTixRegistration.submit();
            return true;
      }

HTML

<tr>
      <td colspan=3 class="reg10" valign="top" bgcolor=#ABDAE0>
            Is this a touring production?
      </td>
      <td class="reg10" valign="top" bgcolor=#ABDAE0>
            <input type="radio" id="touring" name="touring" value=1> &nbsp; Yes &nbsp;&nbsp;
            <input type="radio" id="touring" name="touring" value=0> &nbsp; No
      </td>
</tr>
0
Comment
Question by:eseyfried
  • 3
3 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 350 total points
ID: 11823548
if you have more than one radio with the same name, then you need to treat it as an array:

str = document.frmLAStageTixRegistration.touring[0].value
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 11823578
but I think what you want is not the value of the first radio, but the value of the checked one, correct?

if so ...

var checkedIndex = -1;

if ( document.frmLAStageTixRegistration.touring[0].checked==true )
   checkedIndex = 0;
else if ( document.frmLAStageTixRegistration.touring[1].checked==true )
   checkedIndex = 1;

if ( checkedIndex > -1 )
{
   str = document.frmLAStageTixRegistration.touring[checkedIndex].value;

...
}
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 11823601
actually, you can do it this way:

var checkedIndex = -1;

if ( document.frmLAStageTixRegistration.touring[0].checked==true )
   checkedIndex = 0;
else if ( document.frmLAStageTixRegistration.touring[1].checked==true )
   checkedIndex = 1;

if ( checkedIndex == -1 )
{
      alert("Please answer if this is a touring production or not. " + str);
      document.frmLAStageTixRegistration.touring.focus()
      return false;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now