Javascript Reference - HTML DOM Input URL name Property








The name property sets or gets the value of the name attribute of a URL field.

The name attribute identifies form data after it has been submitted to the server.

We can 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 = urlObject.name

Set the name property.

urlObject.name=name




Property Values

Value Description
name Set the name of the URL field

Return Value

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

Example

The following code shows how to change the name of a URL field.


<!DOCTYPE html>
<html>
<body>
<!--  w w  w . j  a va 2 s. c  o  m-->
Homepage: <input type="url" id="myURL" name="website">
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>

<script>
function display() {
    var x = document.getElementById("myURL").name;
    console.log("The name is: " + x);
}

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

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the name of a URL field.


<!DOCTYPE html>
<html>
<body>
<!-- w ww .j  a  v a  2s  .c  om-->
Homepage: <input type="url" id="myURL" name="website">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myURL").name;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: