Hard worker
Hard worker

Reputation: 4066

How to add focus listening using jquery to every input box?

Here is my attempt to add a listener which clears the placeholder on focus for every input box. Are there any errors evident in my code?

var $inputs = $('.add-item-form :input');
$inputs.each(function() {
    var that = $(this);
    that.focus(function() {
        that.attr('placeholder') = '';
    })
});​

Upvotes: 1

Views: 749

Answers (4)

Govind Malviya
Govind Malviya

Reputation: 13753

Use

that.attr('placeholder','');

insted of

that.attr('placeholder')='';

full code

 var $inputs = $('.add-item-form :input');
        $inputs.each(function() {
            var that = $(this);
            that.focus(function() {
                that.attr('placeholder','');
            })  
        });

as gdoron said, no need to cache dom objects, you can use gdoron's code much cleaner code

Upvotes: 2

laxris
laxris

Reputation: 481

$('.add-item-form:input').focus(function() {
    $(this).attr('placeholder', '');
});​

as gdoron suggested in addition you have to delete the extra space on jquery selector this: '.add-item-form:input' instead of: '.add-item-form :input'

Upvotes: 0

gdoron
gdoron

Reputation: 150273

$('.add-item-form :input').focus(function() {
    $(this).attr('placeholder', '');
});​

Upvotes: 2

thecodeparadox
thecodeparadox

Reputation: 87073

$('.add-item-form :input').each(function() {
     var that = $(this);
     that.on('focus', function() {
         that.attr('placeholder','');
     });  
});

Upvotes: 0

Related Questions