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?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178
  • 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! – Just code Oct 30 '13 at 07:27

10 Answers10

46

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

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
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'" />
C_J
  • 404
  • 4
  • 6
20

CSS worked for me:

input:focus::-webkit-input-placeholder {
    color: transparent;
}
mintedsky
  • 1,073
  • 13
  • 18
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");
    });
});
hadi
  • 31
  • 5
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'" />
Saani
  • 791
  • 7
  • 28
  • 68
0
 $('*').focus(function(){
  $(this).attr("placeholder",'');
  });
Ankit Tyagi
  • 2,381
  • 10
  • 19
0

Try This hope it helps

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

});
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
0
$('input').focus(function()
{
  $(this).attr('placeholder','');

});
vikaskimt
  • 56
  • 6
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.

Rudy
  • 2,323
  • 1
  • 21
  • 23
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);
    }
});
Nerjuz
  • 1,063
  • 12
  • 17