The name
property sets or gets the name attribute of a text field.
The name
attribute identifies
form data after it has been submitted to the server.
We can also use name value to reference form data using JavaScript on the client side.
name |
Yes | Yes | Yes | Yes | Yes |
Return the name property.
var v = textObject.name
Set the name property.
textObject.name=name
Value | Description |
---|---|
name | Set the name of the text field |
A String type value representing the name of the text field.
The following code shows how to change the name of a text field.
<!DOCTYPE html>
<html>
<body>
Username: <input type="text" id="myText" name="abc">
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>
<!-- w w w . java 2 s . c o m-->
<script>
function display() {
var x = document.getElementById("myText").name;
console.log("The name is: " + x);
}
function change() {
var x = document.getElementById("myText").name = "username";
console.log("The name was changed to: " + x);
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to get the name of a text field.
<!DOCTYPE html>
<html>
<body>
<!--from ww w.ja v a2 s . c o m-->
Name: <input type="text" id="myText" name="fname" value="abc"><br>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myText").name;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows: