Button name Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Button


The name property sets or gets the name attribute of a button.

Set the name property with the following Values

Value Description
name Sets the name of the button

Return Value

A String, representing the name of the button

The following code shows how to return the value of the name attribute of a button:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<button id="myBtn" name="myname">My Button</button>

<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>

function display() {
    var x = document.getElementById("myBtn").name;
    console.log("The name of the button is: " + x);

function change() {/*from w  w w  .j  a  v  a  2s . c o m*/
    var x = document.getElementById("myBtn").name = "newButtonName";
    console.log("The name was changed to: " + x);


Related Tutorials