Javascript DOM HTML Input Search name Property set

Introduction

Change the name of a search field:

document.getElementById("mySearch").name = "newSearchName";

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

View in separate window

<!DOCTYPE html>
<html>
<body>

Search: <input type="search" id="mySearch" name="googlesearch">
<p id="demo"></p>
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>

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

function change() {//from  w  ww . j a va 2  s.c  o m
  var x = document.getElementById("mySearch").name = "newSearchName";
  document.getElementById("demo").innerHTML = "The name was changed to: " + x;
}
</script>

</body>
</html>



PreviousNext

Related