HTML DOM Input Text placeholder property

HTMLWeb DevelopmentFront End Technology

The HTML DOM Input Text placeholder property is used for setting or returning the placeholder attribute value of an input text field. The placeholder property is used for giving the web page users a hint about the input element by showing a text inside the input field before the user inputs anything. The placeholder text is greyed by default and isn’t submitted to the form unlike the value property.

Syntax

Following is the syntax for −

Setting the placeholder property −

textObject.placeholder = text

Here, text represents the placeholder text specifying the hint for the user about the text field.

Example

Let us look at an example for the input Text placeholder property −

<!DOCTYPE html>
<html>
<body>
<h1>Input Text placeholder property</h1>
USERNAME: <input type="text" id="USR" placeholder="....">
<p>Change the placeholder text of the above field by clicking the below button</p>
<button onclick="changeHolder()">CHANGE</button>
<script>
   function changeHolder() {
      document.getElementById("USR").placeholder = "Enter your username here..";
   }
</script>
</body>
</html>

Output

This will produce the following output −

Placeholder changed successfully −

raja
Published on 19-Aug-2019 12:33:45
Advertisements