Skip to content Skip to sidebar Skip to footer

Unsure About .on() Method

I was using the $.click() method for triggering some events. But then I needed to set some events for some HTML elements before the elements were declared. Let's take this as an ex

Solution 1:

You missed the three-argument version:

$('body').on('click', 'div.hide', function() { ... });

That puts the handler on the <body> but it catches the events that bubble and invokes the handler when the target element matches the selector (the second argument).

The handler doesn't have to go on the <body>; it can be any parent container element of your <div>.

The "on" method replaces the older "live" and "delegate" methods, as well as "bind" itself.

Solution 2:

Use jQuery.live method to attach an event handler for all elements which match the current selector, now and in the future

Post a Comment for "Unsure About .on() Method"