Javascript Reference - HTML DOM Input Email name Property








The name property sets or gets the name attribute of an email field.

The name attribute from input email element identifies form data after submitting.

We can also use name value to reference form data using JavaScript on the client side.

Browser Support

name Yes 10.0 Yes Yes Yes

Syntax

Return the name property.

var v = emailObject.name

Set the name property.

emailObject.name=name




Property Values

Value Description
name Set the name of the email field

Return Value

A String type value representing the name of the email field.

Example

The following code shows how to get the name of an email field.


<!DOCTYPE html>
<html>
<body>
<!-- w  ww.ja v  a  2  s.co m-->
E-mail: <input type="email" id="myEmail" name="usremail">
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myEmail").name;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the name of an email field.


<!DOCTYPE html>
<html>
<body>
<!--from w w  w .  java 2 s .c om-->
E-mail: <input type="email" id="myEmail" name="usremail">
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>
<script>
function display() {
    var x = document.getElementById("myEmail").name;
    console.log("The name is: " + x);
}

function change() {
    var x = document.getElementById("myEmail").name = "newEmailName";
    console.log ("The name was changed to: " + x);
}
</script>

</body>
</html>

The code above is rendered as follows: