Doubt about jquery

0

Some time ago I have doubts about how it is better to establish the functions of jquery, besides a curious doubt:

In certain parts of the program that I am doing, I call the elements, for example:

$(document).on('click', '.accountNotification', function(){
   alert("lalala");
});

and this works perfectly but nevertheless in other parts it does not work for me and I am forced to call it like this:

$('#finishProjectOption').on('click', function(){
   alert("holi");
});
    
asked by Korzan 19.09.2018 в 12:58
source

1 answer

0

As far as I understand, in the first case you link the event handler with the document and tell it that when you click on the document, any corresponding element with the selector that you have indicated, whether or not the DOM at the time of defining the controller, respond to it

On the other hand, in the second case you link the controller directly with the elements that meet the selector and are in the DOM on that moment when defining the controller

The problem you have with the first case could be because the propagation of the event has stopped at some point ( stopPropagation )

    
answered by 19.09.2018 в 13:40