Javascript Reference - HTML DOM Input Search value Property








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

Browser Support

value Yes Yes Yes Yes Yes

Syntax

Return the value property.

var v = searchObject.value

Set the value property.

searchObject.value=text

Property Values

Value Description
text Set the value of the search field




Return Value

A String type value representing the default value of the search field.

Example

The following code shows how to get the text of a search field.


<!DOCTYPE html>
<html>
<body>
<!--   w  ww.j a  v  a2 s. c o m-->
Search: <input type="search" id="mySearch" value="Food">
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("mySearch").value;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the text of a search field.


<!DOCTYPE html>
<html>
<body>
<!--   w  w w .  j av  a  2 s.co m-->
Search: <input type="search" id="mySearch" value="Food">
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("mySearch").value = "new value";
}
</script>

</body>
</html>

The code above is rendered as follows: