Javascript Reference - HTML DOM Input Text placeholder Property








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

The placeholder attribute specifies a short hint text that describes the expected value of a text field.

Browser Support

placeholder Yes 10 Yes Yes Yes

Syntax

Return the placeholder property.

var v = textObject.placeholder

Set the placeholder property.

textObject.placeholder=text




Property Values

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

Return Value

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

Example

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


<!DOCTYPE html>
<html>
<body>
<!--from w  w  w. ja v a2  s  .c om-->
First Name: <input type="text" id="myText" placeholder="Name">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myText").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 text field.


<!DOCTYPE html>
<html>
<body>
<!--from  w  w  w  .j ava2 s  . co m-->
First Name: <input type="text" id="myText" placeholder="Name">
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myText").placeholder = "first name here..";
}
</script>

</body>
</html>

The code above is rendered as follows: