Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    Bahamas
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unable to get radiobutton values

    This script gives always gives an oujtput of 'undefined' got the radiobutton values.. can anyone see why?



    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript">
    function showgame()
    {
    var frm = document.forms['form1'];
    frm.textfield.value = frm.RG1.value;

    }

    </script>
    </head>

    <body>
    <p>&nbsp; </p>
    <form name="form1" method="post" action="">
    <p>&nbsp;</p>
    <p>
    <label></label>
    </p>
    <p>
    <label>
    <input type="radio" name="RG1" value="D">
    DTC</label>
    Input
    <input type="text" name="textfield">
    <br>
    <label>
    <input type="radio" name="RG1" value="M">
    MIA</label>
    <br>
    <label>
    <input type="radio" name="RG1" value="L">
    LNC</label>
    </p>
    <p>
    <input type="Button" name="Button" value="Button" onClick="showgame()">
    </p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    </form>
    <p>
    <label></label>
    </p>
    <p>
    <label></label>
    <br>
    </p>
    <p>&nbsp; </p>
    </body>
    </html>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    html controls with same name becomes an array. So you access it like frm.RG1[0].value, frm.RG1[1].value, and so on. But what you want is the value of the checked button because values of unchecked radios are not changed.
    Code:
    function showgame()
    {
      var frm = document.forms['form1'];
      for (var i=0;i<frm.RG1.length;i++){
        if (frm.RG1[ i ].checked) {
           frm.textfield.value = frm.RG1[ i ].value;
           break;
        }
      }
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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