Javascript Reference - HTML DOM Input Button name Property








The name attribute from input button element identifies form data after it has been submitted to the server.

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

The name property sets or gets the value of the name attribute of an input button.

Browser Support

name Yes Yes Yes Yes Yes

Syntax

Return the name property.

var v = buttonObject.name

Set the name property.

buttonObject.name=name




Property Values

Value Description
name Set the name of the button

Return Value

A String type value representing the name of the input button.

Example

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


<!DOCTYPE html>
<html>
<body>
<input type="button" id="myBtn" name="myname" value="My Button">
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>
<script>
function display() {
    var x = document.getElementById("myBtn").name;
    console.log("The name is: " + x);
}<!--from  ww w  .  ja va  2  s  . c o m-->

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

</body>
</html>

The code above is rendered as follows: