Get the name of a text field:
var x = document.getElementById("myText").name;
Click the button to display the value of the name attribute of the text field.
<!DOCTYPE html> <html> <body> Name: <input type="text" id="myText" name="fname" value="Mickey"><br> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {/*from w w w . j a va 2s.c o m*/ var x = document.getElementById("myText").name; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The name property sets or gets the value of the name attribute of a text field.
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.