jquerydomevent-handlingjquery-delegate

Am I understanding the jQuery delegate function correctly?


I know that a div will appear at some point in my page's future DOM, and I know the id of it (it's generated by SharePoint javascript) - is it possible for me to use jQuery delegate to attach an event handler to this div prior to it existing in the DOM?

Also, how does this compare to .live() for this particular scenario?


Solution

  • Short answer: Yes

    Long answer: As of jQuery 1.7+ .on() is prefered before the two you have mentioned, they are deprecated. This is an example on .on():

        $('#parent').on("click", "span.children", function() {
            if ( confirm("Are you sure?") ) {
                console.log(this);
            }
        });