The value
property sets or gets the value attribute of a search field.
value |
Yes | Yes | Yes | Yes | Yes |
Return the value property.
var v = searchObject.value
Set the value property.
searchObject.value=text
Value | Description |
---|---|
text | Set the value of the search field |
A String type value representing the default value of the search field.
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:
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: