Javascript DOM HTML Input Search defaultValue Property set

Introduction

Change the default value of a search field:

document.getElementById("mySearch").defaultValue = "CSS";

Click the button to change the default value of the search field.

View in separate window

<!DOCTYPE html>
<html>
<body>

Search: <input type="search" id="mySearch" value="Food">
<button type="button" onclick="myFunction()">Test</button>

<script>
function myFunction() {/*from   ww  w  .j a v a 2s.  com*/
  document.getElementById("mySearch").defaultValue = "CSS";
}
</script>

</body>
</html>

The defaultValue property sets or gets the default value of a search field.

The default value is the value set in the HTML value attribute.

The defaultValue contains the default value, while value contains the current value.

If there are no changes, defaultValue and value is the same.




PreviousNext

Related