Return the value of the name attribute of a button:
var x = document.getElementById("myBtn").name;
Click the button to return the value of its name attribute.
<!DOCTYPE html> <html> <body> <button id="myBtn" name="myname" onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {//from w ww .j ava 2 s . co m var x = document.getElementById("myBtn").name; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The name property sets or gets the value of the name attribute of a button.
The name attribute sets the name of a button and is used to reference form data after it has been submitted.