First method
You can use javascript, I took the Focus and Seto event the value in the placeholder and onblur to return to the previous value, and you choose which element you want to do this effect using querySelector.
var elemento = document.querySelector("input");
elemento.onfocus = function(){
elemento.placeholder = "apenas número";
};
elemento.onblur = function(){
elemento.placeholder = "telefone";
};
Quando focar nesse inpute gostaria de trocar o texto do placeholder para por exemplo "apenas números"
<br><br>
<input type="tel" placeholder="telefone" name="" id="">
Second method
I created a new way in which you will only worry about javascript once, just put the class alter-placeholder in the element and the attributes placeholder-Focus and placeholder-Blur defining the text, see an example:
var elemento = document.querySelector(".alter-placeholder");
elemento.onfocus = function(){
elemento.placeholder = this.getAttribute('data-placeholder-focus');
};
elemento.onblur = function(){
elemento.placeholder = this.getAttribute('data-placeholder-blur');
};
Quando focar nesse inpute gostaria de trocar o texto do placeholder para por exemplo "apenas números"
<br><br>
<input
placeholder="telefone"
data-placeholder-blur="telefone"
data-placeholder-focus="apenas número"
class="alter-placeholder"
type="tel" name="" id="">
Vitor thanks for the contribution, I believe that this code will help me even on other occasions! []s
– hugocsl
I would recommend using
data-placeholder-blur
anddata-placeholder-focus
forplaceholder-blur
andplaceholder-focus
are not valid attributes, making your page an invalid HTML5 document.– fernandosavio
Thanks @fernandosavio, you’re right, I edited my reply
– Wictor Chaves