Javascript DOM HTML Input Button name Property set

Introduction

Get the name of a button:

Change the name of a button:

document.getElementById("myBtn").name = "newButtonName";
<p>Click the buttons to display/change the name attribute of the input button.

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="button" id="myBtn" name="myname" value="My Button">
<p id="demo"></p>
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>

<script>
function display() {
  var x = document.getElementById("myBtn").name;
  document.getElementById("demo").innerHTML = "The name of the input button is: " + x;
}

function change() {//from w  w w  .  j  a v  a  2  s . c om
  var x = document.getElementById("myBtn").name = "newButtonName";
  document.getElementById("demo").innerHTML = "The name was changed to: " + x;
}
</script>

</body>
</html>



PreviousNext

Related