[Solved] .on method in jquery [closed]


$(function(){
    $(document).on('click','.mydiv', function(){ //my code});
});

When delegating events you listen for any click on an element that exists on the time of binding, as event handlers can only be attached to elements that actually exists in the DOM on the time of binding, this would be the first element in the statement above, and then it checks to see if the target (the one that was clicked) element matches the element you’re really listening for, the second element in the statement above.
document always exists, but using an element closer to the actual element would be better.

It listens for any click on the document, which is the entire page as all elements are within the document, and then checks to see if the click originated from the second element in the statement, and if it did it fires the function. Obviously the closer you get to the actual element the fewer elements will fire the click function and be filtered out.

solved .on method in jquery [closed]