Connecting Tech Pros Worldwide Forums | Help | Site Map

Get radio button values to javascript

Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#1: Jul 2 '09
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?
Expand|Select|Wrap|Line Numbers
  1. php code
  2. --------------
  3. <input name="Gender" id="Gender" type="radio" value="1" /> Male
  4. <input name="Gender" id="Gender" type="radio" value="2" />Female&nbsp;
  5.  
  6. javascript code
  7. -------------------
  8. var Gender=document.getElementById('Gender').value;
  9.  
  10.  

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#2: Jul 2 '09

re: Get radio button values to javascript


Quote:

Originally Posted by ghjk View Post

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.

Quote:

Originally Posted by ghjk View Post

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).
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#3: Jul 5 '09

re: Get radio button values to javascript


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