MR.Don't know
MR.Don't know

Reputation: 235

Jquery add class to input

i got this jquery script, it puts placeholder "+" in my search input, and probblem is that, now every input got "+" placeholder on my website,

how to add only particulary class to this js code:

$( document ).ready(function() {
    $('input').on('focus',function(){
    $(this).attr('placeholder',"");
});
$('input').on('blur',function(){
    $(this).attr('placeholder',"+");
});

});

Upvotes: 0

Views: 355

Answers (2)

Alex
Alex

Reputation: 9031

This is quite simple jQuery code. A google search would have found you an answer. You just need to specify the class name:

$( document ).ready(function() {
  $('input.CLASSNAME').on('focus',function(){
    $(this).attr('placeholder',"");
  });
  $('input.CLASSNAME').on('blur',function(){
    $(this).attr('placeholder',"+");
  });
});

Upvotes: 1

brk
brk

Reputation: 50291

Use an identifier like class or id where you want to update the placeholder

Hope this snippet will be usefull

JS

$( document ).ready(function() {
    $('input').on('focus',function(){
    $(this).attr('placeholder',"");
});
// add placeholder to input which have class updatePlaceholder
$('input.updatePlaceholder').on('blur',function(){
    $(this).attr('placeholder',"+");
});

});

HTML

<input type = "text">
<input type = "text" class = "updatePlaceholder">

Check this jsFiddle

Upvotes: 0

Related Questions