Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the selected radio button value using js

I am using this code to get the value of currently selected radio button, but it doesn't work.

var mailcopy = document.getElementById('mailCopy').value;  

How to get the currently selected radio button value using Javascript?

like image 652
Meena Avatar asked Oct 06 '10 04:10

Meena


People also ask

How can I know which radio button is selected via Javascript?

Use document. getElementById('id'). checked method to check whether the element with selected id is check or not. If it is checked then display its corresponding result otherwise check the next statement.

How do I know which radio button is selected?

To find the selected radio button, you follow these steps: Select all radio buttons by using a DOM method such as querySelectorAll() method. Get the checked property of the radio button. If the checked property is true , the radio button is checked; otherwise, it is unchecked.

Which method can be used to obtain the value associated with the selected radio button in Python?

If we want to get the output to check which option the user has selected, then we can use the get() method. It returns the object that is defined as the variable.


2 Answers

HTML

<p>Gender</p> <input type="radio" id="gender0" name="gender" value="Male">Male<br> <input type="radio" id="gender1" name="gender" value="Female">Female<br> 

JS

var gender = document.querySelector('input[name = "gender"]:checked').value; document.writeln("You entered " + gender + " for your gender<br>"); 
like image 199
Mihai Alin Avatar answered Sep 23 '22 15:09

Mihai Alin


If you are using jQuery, following code will work for you.

$('input[name=radioName]:checked').val(); 
like image 44
Chamika Sandamal Avatar answered Sep 21 '22 15:09

Chamika Sandamal