Last Updated: April 12, 2019
·
1.466K
· pixelclear

Faster $(document).ready()

Forget $(document).ready() and start using .live

$('#id').live('click', function () {
    $(this).next().slideToggle();
});

Less code, improves performance and doesn't leave you waiting.

Update: .live is deprecated, but is still useful if your stuck in an older version of jQuery. Not every site is on the edge. Latest jQuery sites should use .on

I also recommend reading Edmund Kump's thoughts on this http://coderwall.com/p/bovyfw

1 Response
Add your response

It's good to note that while .live used to work with every element .on only works with element that existed before the .on function was defined.

So in the case of adding dynamic elements it's necessary to attach the event to an existing element (a parent of the dynamic ones, it may be event the document but it's better for performance the closest to the dynamic elements).

Then we need to filter our dynamic element with an additional parameter to .on like: $('.container').on('click','a',function(){...}); This example adds the event "click" to all tags "a" inside the ".container" element.

over 1 year ago ·