How to remove placeholder on focus [duplicate]
Asked Answered
M

10

25

I made this simple function to add placeholder in browsers that do not support it:

DEMO

The question is: How can I add to that function the possibility to remove the placeholder when the user click inside it?

Markswoman answered 30/10, 2013 at 7:18 Comment(1)
Why you are using place holder for this one you can use blur also placeholder is the inbuilt attributes of browser ie does not support this remeber!Cuprite
D
47

Try to use removeAttr() like,

$('input,textarea').focus(function(){
   $(this).removeAttr('placeholder');
});

Demo

To get the placeholder value again on blur() try this,

$('input,textarea').focus(function(){
   $(this).data('placeholder',$(this).attr('placeholder'))
          .attr('placeholder','');
}).blur(function(){
   $(this).attr('placeholder',$(this).data('placeholder'));
});

Demo 1

Dwyer answered 30/10, 2013 at 7:19 Comment(5)
Thank you Rohan. It is working properly in Chrome. Altough is not working in Explorer (v. 8), that is where mostly i need this function. Do you know how can i do to make this code working also on Explorer 8? Thank you!Markswoman
If you need i have create a new JSBin: jsbin.com/avIfuDAf/1Markswoman
@johnnyfittizio this is because placeholder not works in IE 8, for this you have to set value of your elements on focus and blur See demo jsbin.com/avIfuDAf/3Dwyer
How can exactly do that in the code you post? Thanks!Markswoman
Thank you Rohan! Somehow i have missed tour last jsbin.. It works perfectly!Markswoman
V
36

There is no need to use javascript function to accomplish this, a simpler solution is:

<input type="text" placeholder="enter your text" onfocus="this.placeholder=''" onblur="this.placeholder='enter your text'" />
Vedavedalia answered 5/9, 2014 at 23:49 Comment(2)
Nice one! it is working perfectlySall
@Vedavedalia this is brilliant..Kerrin
N
20

CSS worked for me:

input:focus::-webkit-input-placeholder {
    color: transparent;
}
Nunnally answered 25/7, 2014 at 8:18 Comment(3)
Best solution for modern browsers.Storz
Any Solution for FireFoxMatthew
Nilesh: css-tricks.com/almanac/selectors/p/placeholderErythema
F
1
$("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");
    });
});
Flocky answered 28/2, 2015 at 9:56 Comment(0)
T
1

A very simple and comprehensive solution for this is which works in Mozila, IE, Chrome, Opera and Safari:

<input type="text" placeholder="your placeholder" onfocus="this.placeholder=''" onblur="this.placeholder='your placeholder'" />
Tiber answered 12/8, 2015 at 4:38 Comment(0)
V
0
 $('*').focus(function(){
  $(this).attr("placeholder",'');
  });
Veta answered 30/10, 2013 at 7:20 Comment(0)
R
0

Try This hope it helps

$('input,textarea').focus(function()
{
$(this).attr('placeholder','');

});
Rescission answered 30/10, 2013 at 7:25 Comment(0)
S
0
$('input').focus(function()
{
  $(this).attr('placeholder','');

});
Shuffle answered 30/10, 2013 at 7:27 Comment(0)
L
0

For browsers that don't support placeholder you can use this: https://github.com/mathiasbynens/jquery-placeholder. Add the placeholder attribute normally as for HTML5, then call this plugin: $('[placeholder]').placeholder();. Then use Rohan Kumar's code and will be cross-browser.

Lorie answered 30/10, 2013 at 7:39 Comment(0)
S
0

This is my solution on click not on focus:

$(document).on('click','input',function(){
    var $this = $(this);
    var place_val = $this.attr('placeholder');
    if(place_val != ''){
        $this.data('placeholder',place_val).removeAttr('placeholder');
    }
}).on('blur','input',function(){
    var $this = $(this);
    var place_val = $this.data('placeholder');
    if(place_val != ''){
        $this.attr('placeholder',place_val);
    }
});
Sulk answered 28/1, 2016 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.