Javascript Reference - HTML DOM Textarea placeholder Property








The placeholder property sets or gets the placeholder attribute of a textarea.

The placeholder attribute sets a short hint text that describes the expected value of a textarea.

Browser Support

placeholder Yes 10.0 Yes Yes Yes

Syntax

Return the placeholder property.

var v = textareaObject.placeholder

Set the placeholder property.

textareaObject.placeholder=text




Property Values

Value Description
text Set a short hint text that explains the expected value of the textarea

Return Value

A String type value representing a short hint text that explains the expected value of the textarea.

Example

The following code shows how to change the placeholder text of a textarea.


<!DOCTYPE html>
<html>
<body>
Address:<br>
<textarea id="myTextarea" placeholder="Your address..">
</textarea>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!-- w  w w  . ja v a  2  s  .  co  m-->
    document.getElementById("myTextarea").placeholder = "new message";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

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


<!DOCTYPE html>
<html>
<body>
Address:<br>
<textarea id="myTextarea" placeholder="Your address..">
</textarea>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from   w ww.  j a  va 2s.co m-->
    var x = document.getElementById("myTextarea").placeholder;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: