SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Anywhere
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem with radio button and javascript

    I cannot see the value of the <input type="radio".....
    through the javascript code

  2. #2
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Since you didn't post your code I have no way of knowing what your doing wrong, but try this and see if it sheds any light on your problem.

    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    function getRadioVals(el) {
      var str = '', i = 0;
      for(i; i < el.length; i++)
      {
        str += 
        'Value of radio '+i+': ' 
        + '"' + el[i].value + '"' + 
        '\n\t checked = '
        + el[i].checked + '\n';
      }
      alert(str);
    }
    </script>
    
    </head>
    
    <body>
    
    <form name="f">
    <input type="radio" name="radios" value="val0" /> 1<br />
    <input type="radio" name="radios" value="val1" checked="checked" /> 2<br />
    <input type="radio" name="radios" value="val2" /> 3<br />
    <input type="radio" name="radios" value="val3" /> 4<br />
    </form>
    
    <a href="#" onclick="getRadioVals(document.f.radios); return false;">get Radio Properties</a>
    
    </body>
    </html>
    -xDev

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Anywhere
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script>
    function check() {
    
    var radioValue=document.formName.radios.value;
    
    alert(radioValue);
    }
    </script>
    .....
    ....
    <input type="radio" name="radios" value="1">
    i cannot see the value on the alert function, but only undefined

  4. #4
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dominant
    Code:
    <script>
    function check() {
    
    var radioValue=document.formName.radios.value;
    
    alert(radioValue);
    }
    </script>
    .....
    ....
    <input type="radio" name="radios" value="1">
    i cannot see the value on the alert function, but only undefined
    Radio buttons are collections. They work the same as arrays. What you want is document.formName.radios[0].value

    That's why in my example code I looped through my 4 radio buttons to show you that radio button are indeed arrays. Did you try the code?

    e.g:
    Code:
    <input type="radio" name="radios" value="1">
    <input type="radio" name="radios" value="2">
    If you want to get the value of the second radio button, it would be:
    document.formName.radios[1].value, which is 2!

    Arrays start at index 0, so your first radio button value is always referenced as document.formName.radioButtonGroupName[0].value, or in the case of the above code:

    document.formName.radios[0].value

    Some links for you:
    http://www.devguru.com/Technologies/...ref/radio.html

    http://devedge.netscape.com/library/...nce/radio.html

    http://tech.irt.org/articles/js216/index11.htm

    http://www.jennifermadden.com/162/ex...loopRadio.html

    -xDev
    Last edited by xDev; Jul 29, 2003 at 02:35.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Anywhere
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help!


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
  •