From the documentation
$(selector).live(events, data, handler); // jQuery 1.3+
$(document).delegate(selector, events, data, handler); // jQuery 1.4.3+
$(document).on(events, selector, data, handler); // jQuery 1.7+
I’m using jQuery 1.7.1
This works, for static elements and dynamically loaded elements:
$("input").live("change", function () { alert("hello"); });
This doesn’t work, not even for static elements:
$("document").on("change", "input", function () { alert("hello"); });
What am I missing?
Write it like
You can replace
documentwith anycloser parent elementwhich will always exist inDOMfor better performance. Likeif you use
$("document")jQuery will search for anode/tagnamed asdocumentlike<document>and wont find anything asdocumentis actually anobject.But you could use
$("body")asbodyis a node/element ofDOM.