SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist lance_vincent's Avatar
    Join Date
    Aug 2004
    Location
    philippines
    Posts
    574
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Access the selected Radio element

    Hi guys!

    This is what i would like to accomplish. I have a poll, where the choices are laid out as Radio buttons. they are named "choice". when the form submits, i try to echo the value of the selected choice by

    Code:
    document.forms['pollform'].choice.value
    but it says undefined. it must have something to do with a "selected" keyword, but im not sure what to do.

    will appreciate any form of help guys.

    Thanks!
    If you won't dress like the
    Victoria Secret girls,
    don't expect us to act like soap opera guys.

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a collection of radio buttons, not a single one. It does not work exactly like a select element does.

    Use this.
    Code:
    function getRadioGroupValue(f,grpName) {
      // f should be a reference to a form element
      if(!f || !grpName) return null;
      var grp;
      if(f.getElementsByName) grp = f.getElementsByName(grpName);
      else if(f.elements) grp = f.elements[grpName];
      else return null;
      for(var i=0,len=grp.length;i<len;i++) {
        if(grp[i].checked) return grp[i].value;
      }
      return null;
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Evangelist lance_vincent's Avatar
    Join Date
    Aug 2004
    Location
    philippines
    Posts
    574
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    great! thanks! let me see how will this fit in my code!
    If you won't dress like the
    Victoria Secret girls,
    don't expect us to act like soap opera guys.


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
  •