SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Philadelphia
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need to find VALUE of multiple occuring form name element

    In my form, the user can check only one option: either
    * ship to default address
    * ship to new address

    Both options are radio buttons. They both have the same instance name, but different ids. That way the user is forced to choose either one or the other.

    My problem is that since both radio buttons have the same instance name, then I cannot get the value of the instance with JavaScript.

    <input name="shipchoice" id="shipchoice1" type="radio" class="style1" value="default" checked>.

    <input name="shipchoice" id="shipchoice2" type="radio" class="style1" value="new">

    Can you see the problem? If I give them both different instance names, then I can get the value of "shipchoice" with:

    var cosa;
    cosa=document.myForm.shipchoice.value;

    but of course if there are two "shipchoice" instances, then I CANNOT access the shipchoice variable....it comes up "undefined".
    Last edited by Shem; Nov 10, 2004 at 15:40.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this help:

    Code:
    <!DOCTYPE html
      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    
    <html xmlns="http://www.w3.org/1999/xhtml" 
      xml:lang="en" lang="en">
    
    <head>
    <title>html and javascript</title>
    <script type="text/javascript" language="javascript">
    <!-- Hide from browsers lacking javascript
    
    window.onload=function()
    {
    	document.getElementById("button").onclick=display_checked;
    	
    };
    
    function display_checked()
    {
    	if(document.getElementById("shipchoice1").checked==true)
    		alert(document.getElementById("shipchoice1").value);
    	else
    		alert(document.getElementById("shipchoice2").value);
    }
    		
    // End hiding -->
    </script>
    </head>
    <body>
    
    <div><input name="shipchoice" id="shipchoice1" type="radio" class="style1" value="default" checked="checked">default address</div>
    
    <div><input name="shipchoice" id="shipchoice2" type="radio" class="style1" value="new">new address</div>
    
    <input type="button" id="button" value="get checked" />
    
    </body>
    </html>

  3. #3
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Philadelphia
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    YES it does. getElementById ....just what the doctor ordered.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •