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?
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.
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.
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.
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>");
If you are using jQuery, following code will work for you.
$('input[name=radioName]:checked').val();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With