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
Solved

Getting an undifined from input with a type=radio

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

840 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