qinHaiXiang
qinHaiXiang

Reputation: 6419

Which way is better to find and bind element with event in JQuery?

E.g:

$('.container').find('specialElement').bind('event',function(){...});

or

$('.container').delegate('specialElement','event',function(){...});

So,which one is better or both of them are good?

Thank you very much!!

Upvotes: 0

Views: 49

Answers (2)

Jeremy Battle
Jeremy Battle

Reputation: 1638

$('.container').delegate('specialElement','event',function(){...});

Would be better if you want the event to work for elements added to the page after load. However, keep in mind if you use delegate it is not possible to stop the propagation of other events with the event specified in the delegate function.

Upvotes: 1

generalhenry
generalhenry

Reputation: 17319

$('.container specialElement').bind('event',function(){...});

also works

the first on is better if the element exists for the whole duration of use the second works if you dynamically add the specialElement

Upvotes: 1

Related Questions