Javascript Reference - HTML DOM Input Number placeholder Property








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

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

Browser Support

placeholder Yes 10.0 Yes Yes Yes

Syntax

Return the placeholder property.

var v = numberObject.placeholder

Set the placeholder property.

numberObject.placeholder=text




Property Values

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

Return Value

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

Example

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


<!DOCTYPE html>
<html>
<body>
<!--from w  w  w  .  ja v  a 2  s .c  o m-->
<input type="number" id="myNumber" placeholder="Favorite Number">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myNumber").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 number field.


<!DOCTYPE html>
<html>
<body>
<!-- w w  w. j  a v  a 2s . c o  m-->
<input type="number" id="myNumber" placeholder="Quantity">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    document.getElementById("myNumber").placeholder = "Amount";
}
</script>
</body>
</html>

The code above is rendered as follows: