Skip to main content

Toni's answer is good, but I'd rather drop the IDID and explicitly use "input"input, that way all inputs with placeholdersplaceholder 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')); }); }) 

http://jsfiddle.net/dreamyguy/6CzRq/5/Demo.

Toni's answer is good, but I'd rather drop the ID and explicitly use "input", that way all inputs with placeholders 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')); }); }) 

http://jsfiddle.net/dreamyguy/6CzRq/5/

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.

Source Link
Wallace Sidhrée
  • 11.7k
  • 6
  • 53
  • 58

Toni's answer is good, but I'd rather drop the ID and explicitly use "input", that way all inputs with placeholders 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')); }); }) 

http://jsfiddle.net/dreamyguy/6CzRq/5/