I hvae the following code:
$(".openDialog").live("click", function (e) {
e.preventDefault();
$("<div></div>")
.addClass("dialog")
.attr("id", $(this).attr("data-dialog-id"))
.appendTo("body")
.dialog({
title: $(this).attr("data-dialog-title"),
close: function () { $(this).remove() },
modal: true
})
.load(this.href);
});
$(".close").live("click", function (e) {
e.preventDefault();
$(this).closest(".dialog").dialog("close");
});
Can someone explain how I can decouple the functions form the actions? I am a bit confused with how to do this.
Also what is the purpose of “live” ? I heard before someone suggesting “on”. Is “on” better than “live” and how does it actually work?
Just break the functions out, then pass the function name:
onis indeed better thanlive. Both allow you to wire up events to dynamically added content, but the former is more efficient, and the latter is deprecated, . Here’s how you’d use itor, ideally, if all these
.closebuttons were to be in a container, say, a div withidfoo, you could more efficiently door for jQuery pre 1.7 you’d have to settle for
delegate