Connecting Tech Pros Worldwide Help | Site Map

Get radio button values to javascript

  #1  
Old July 2nd, 2009, 09:00 AM
Familiar Sight
 
Join Date: Jan 2008
Posts: 196
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.  

Last edited by Dormilich; July 2nd, 2009 at 09:02 AM. Reason: fixed [code] tags
  #2  
Old July 2nd, 2009, 09:05 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

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).
  #3  
Old July 5th, 2009, 03:53 AM
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
Provided Answers: 2

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
AJAX -- passing on radio button values ToddFur answers 3 January 18th, 2009 10:33 AM
Adding values from radio button & select box crayfiss answers 8 January 10th, 2008 11:50 AM
php and Javascript call to change radio button IchBin answers 10 December 7th, 2006 08:15 AM
Please tell me the generic way to check different radio button groups? tabonni answers 15 July 23rd, 2005 01:55 PM