This is done automatically for every browser except Chrome.
I\'m guessing I have to specifically target Chrome.
Any solutio
for input
input:focus::-webkit-input-placeholder { color:transparent; }
input:focus:-moz-placeholder { color:transparent; }
for textarea
textarea:focus::-webkit-input-placeholder { color:transparent; }
textarea:focus:-moz-placeholder { color:transparent; }
$("input[placeholder]").each(function () {
$(this).attr("data-placeholder", this.placeholder);
$(this).bind("focus", function () {
this.placeholder = '';
});
$(this).bind("blur", function () {
this.placeholder = $(this).attr("data-placeholder");
});
});
Toni's answer is good, but I'd rather drop the ID
and explicitly use input
, that way all inputs with placeholder
get the behavior:
<input type="text" placeholder="your text" />
Note that $(function(){ });
is the shorthand for $(document).ready(function(){ });
:
$(function(){
$('input').data('holder',$('input').attr('placeholder'));
$('input').focusin(function(){
$(this).attr('placeholder','');
});
$('input').focusout(function(){
$(this).attr('placeholder',$(this).data('holder'));
});
})
Demo.
The same thing i have applied in angular 5.
i created a new string for storing placeholder
newPlaceholder:string;
then i have used focus and blur functions on input box(i am using prime ng autocomplete).
Above placeholder is being set from typescript
Two functions i am using -
/* Event fired on focus to textbox*/
Focus(data) {
this.newPlaceholder = data.target.placeholder;
this.placeholder = '';
}
/* Event fired on mouse out*/
Blur(data) {
this.placeholder = this.newPlaceholder;
}
No need to use any CSS or JQuery. You can do it right from the HTML input tag.
For example, In below email box, the placeholder text will disappear after clicking inside and the text will appear again if clicked outside.
<input type="email" placeholder="Type your email here..." onfocus="this.placeholder=''" onblur="this.placeholder='Type your email here...'">
2018 > JQUERY v.3.3 SOLUTION: Working globaly for all input, textarea with placeholder.
$(function(){
$('input, textarea').on('focus', function(){
if($(this).attr('placeholder')){
window.oldph = $(this).attr('placeholder');
$(this).attr('placeholder', ' ');
};
});
$('input, textarea').on('blur', function(){
if($(this).attr('placeholder')){
$(this).attr('placeholder', window.oldph);
};
});
});