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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can I store the name of a radio Button

    Hi,

    How can I store the name of a radio button?

    The reason I am asking is that if I have determined that a radio button pair with name "myRadio1" has had the radio button with value "Yes" clicked I want to store the name of that particular radio button pair in an array.

    Kind Regards,

    Matthew

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You may send it to a function using this parameter and even build an array with all the name of radio buttons which were clicked 'YES':

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    script>
    var 
    rad = new Array()
    function 
    storeN(n){
    rad.push(n)
    }
    </script>
    </head>

    <body>
    <form>
    <input name="rad1" type="radio" value="yes" onclick="storeN(this.name)">
    <input name="rad1" type="radio" value="no"><br>
    <br>
    <input name="rad2" type="radio" value="yes" onclick="storeN(this.name)">
    <input name="rad2" type="radio" value="no">
    </form>
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi,
    The following is built on Kor's suggestion, but it also accounts for checking the 'no' button by removing the 'yes' value in the array.

    Code:
    <html>
    <head>
    <script type="text/javascript" language="javascript">
    var rad = new Array() 
    
    function storeN(oRad)
    { 
      var val = oRad.name + "=" + oRad.value
      if(oRad.value=='yes')
        rad.push(val) 
      else
        rad.pop();
    } 
    </script>
    </head>
    <body>
    <form> 
    <input name="rad1" type="radio" value="yes" onclick="storeN(this)"> 
    <input name="rad1" type="radio" value="no" onclick="storeN(this)"><br> 
    <br> 
    <input name="rad2" type="radio" value="yes" onclick="storeN(this)"> 
    <input name="rad2" type="radio" value="no" onclick="storeN(this)"> 
    </form> 
    </body>
    </html>
    The following version is for 2 or more buttons; however, it isn't as 'perfect' as it could be -- removal of an array item leaves a null, so when checking values you need to look only at non-null values

    Code:
    <html>
    <head>
    <script type="text/javascript" language="javascript">
    var rad = new Array() 
    
    function storeN(oRad)
    { 
      var val = oRad.name + "=" + oRad.value
      if(oRad.checked && oRad.value=='yes')
        rad.push(val) 
      else
      {
        radName = oRad.name;
        for (var i = 0; i < rad.length; i++)
        if (rad[i].indexOf(radName) != -1)
           rad[i] = null
      }
     alert(rad)
    } 
    </script>
    </head>
    <body>
    <form> 
    <input name="rad1" type="radio" value="yes" onclick="storeN(this)"> 
    <input name="rad1" type="radio" value="no" onclick="storeN(this)"><br> 
    <br> 
    <input name="rad2" type="radio" value="yes" onclick="storeN(this)"> 
    <input name="rad2" type="radio" value="no" onclick="storeN(this)"> 
    </form> 
    </body>
    </html>
    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Thanks, Vincent, you were well completed my initial ideea.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Quote Originally Posted by Kor
    Thanks, Vincent, you were well completed my initial ideea.
    You're welcome

    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All of these script generate runtime errors in IE 5.0....
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #7
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi james,

    ... generate runtime errors in IE 5.0
    Since I no longer have 5.0, I cannot really state anything with conviction, but it may be the 'push/pop'. To see if it is, replace the function with:

    if(oRad.checked && oRad.value=='yes')
    rad[rad.length] = val;
    else
    {
    radName = oRad.name;
    for (var i = 0; i < rad.length; i++)
    if (rad[i].indexOf(radName) != -1)
    rad[i] = null
    }


    Vinny
    rad[rad.length] =
    Where the world once stood
    the blades of grass
    cut me still

  • #8
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Vinny:

    Thanks for the code...but I was still getting a js error...

    I had to reformat my HD and reinstall Windows today, so I will have to do some more testing and get back to you, once I recover old files....


    Thanks, again...


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #9
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    reformat my HD and reinstall Windows
    ugh!

    Vinny
    Where the world once stood
    the blades of grass
    cut me still


  •  

    Posting Permissions

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