Search code examples
jqueryhtmlcsstextinput

How to add placeholder while focusout for all the fields in form?


$('*').focus(function(){
$(this).removeAttr("placeholder");
});

This is used to work for all the fields in my form. But I'm not able to find how to add.

$('*').focusout(function(){
$(this).attr("placeholder","abc"); 
} 

This is used for an individual field, but I need for all the fields commonly like hide and show


Solution

  • Not verry optimised but it work

    $('*:input').each(function(){
    $(this).data('placeholder',$(this).attr("placeholder"))
    $(this).removeAttr("placeholder");
    $(this).on('focus', function(){
    	$(this).attr("placeholder", $(this).data('placeholder'));
    });
    $(this).on('focusout', function(){
    	$(this).removeAttr("placeholder");
    });
    });