These days i am working on a jquery project and i found that if i use direct event like
$('.selector').click(function(){
});
will work perfect but in case of page partial post back or dynamic content i have to use .on as follow
$('body').on('click','.selector',function(){
});
Now my question is on work on static content as well on dynamic content , so why not i always use on directly instead of static event like click .
Is there any performance issue with .on
Is there any performance issue with .on
There is no performance issue with .on. In fact, your first example is just a shortcut for:
$('.selector').on('click', function(){
The difference as you say is the use of $('body').on delegates the event for elements that are not yet in the DOM which naturally will require more work from the browser. If you need the delegation on the document or body then use that, otherwise use the other way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With