Javascript Reference - HTML DOM Input Search placeholder Property








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

The placeholder attribute sets a short hint text that explains the expected value of a search field.

Browser Support

placeholder Yes 10.0 Yes Yes Yes

Syntax

Return the placeholder property.

var v = searchObject.placeholder

Set the placeholder property.

searchObject.placeholder=text




Property Values

Value Description
text Set a short hint text that describes the expected value of the search field

Return Value

A String type value representing a short hint that describes the expected value of the search field.

Example

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


<!DOCTYPE html>
<html>
<body>
<!--  w  w  w.  ja  v  a  2  s .c  o  m-->
<input type="search" id="mySearch" placeholder="Search for something..">
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("mySearch").placeholder;
    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 placeholder text of a search field.


<!DOCTYPE html>
<html>
<body>
<!--  www .  j a  v  a2s .c om-->
<input type="search" id="mySearch" placeholder="Filter items">
<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("mySearch").placeholder = "Search files";
}
</script>

</body>
</html>

The code above is rendered as follows: