olo
olo

Reputation: 5271

JavaScript jQuery appendTo and plugin work together

I am trying to append an input markup

 $('<input type="text" placeholder="I have a placeholder!">').appendTo('.myclass');

the input field is fine, but placeholder plugin is not working. I use Placeholders.js plugin, So i have to use

setTimeout(function(){$('input').placeholder();},300)

to make it work.

I just wanted to make sure if this is a right way to make it work or there is a better method.

Answer

I found I should put $('input').placeholder(); after appendTo not before. Problem is resolved! Thanks guys

Upvotes: 0

Views: 54

Answers (1)

Jason P
Jason P

Reputation: 27012

Adding to the DOM is synchronous, so you should be able to do this:

$('<input type="text" placeholder="I have a placeholder!">')
    .appendTo('.myclass')
    .placeholder();

Upvotes: 1

Related Questions