Get radio button values to javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    Get radio button values to javascript

    I have 2 radio buttons in php file and I want to send values of them to a javascript. When I put same ID name to both radiobuttions, it doesn't work. But javascript only get values using the id. How can I do that?
    Code:
    php code
    --------------
    <input name="Gender" id="Gender" type="radio" value="1" /> Male
    <input name="Gender" id="Gender" type="radio" value="2" />Female&nbsp;
    
    javascript code
    -------------------
    var Gender=document.getElementById('Gender').value;
    Last edited by Dormilich; Jul 2 '09, 08:02 AM. Reason: fixed [code] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by ghjk
    I have 2 radio buttons in php file and I want to send values of them to a javascript. When I put same ID name to both radiobuttions, it doesn't work.
    of cause not, IDs are supposed to be unique.

    Originally posted by ghjk
    But javascript only get values using the id. How can I do that?
    I doubt that. you should be able to retrieve the values with the various getElementsBy* functions ( * = Name, TagName, ClassName, …). this of course requires working with NodeLists (kind of Arrays).

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Get all radio buttons into an array using getElementsByNa me, and then iterate through it to see if any of the radio buttons is checked. If checked, get value.

      Comment

      Working...