Solved

Getting an undifined from input with a type=radio

Posted on 2004-08-17
3
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular tyeahead feature not working with Control "as" ControlXXX 4 50
Problem in javascript 17 58
Javascript image manipulationcrop-image 7 41
Add Promise to Existing Code 6 31
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

751 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