Solved

Getting an undifined from input with a type=radio

Posted on 2004-08-17
3
317 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

708 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

20 Experts available now in Live!

Get 1:1 Help Now