Javascript DOM HTML Input Text name Property set

Introduction

Change the name of a text field:

document.getElementById("myText").name = "username";

Click the buttons below to display and change the value of the name attribute of the text field.

View in separate window

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
Username: <input type="text" id="myText" name="usrnm">
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>

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

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

</body>
</html>



PreviousNext

Related