Get the name of a radio button:
var x = document.getElementById("myRadio").name;
Click the button to display the value of the name attribute of the radio button.
<!DOCTYPE html> <html> <body> <input type="radio" name="colors" value="red" id="myRadio">Red<br> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {// w ww. j a v a2s .com var x = document.getElementById("myRadio").name; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The name property sets or gets the name attribute of a radio button.
You define radio button groups with the name property.
The radio buttons with the same name belong to the same group.
The name attribute will identify form data after submitting to the server.
Only form elements with a name attribute will be passed to the server.
The name property accepts and returns a String type value.