This gist provides a jQuery plugin for observing events, offering improved flexibility and functionality. The plugin supports various syntaxes, including object-based and chained function-based, and allows for observing both DOM and AJAX events. It also includes error handling and validation for input parameters.
<input id="input">
<select id="select">
<option value="" selected></option>
<option value="1">1</option>
</select>
<div id="div">
<span>div</span>
</div>
$(() => {
$("#input")
.observe("paste", () =>
console.log("Input is pasted!"))
.observe("input", () =>
console.log("Input is input!"))
.observe("change", () =>
console.log("Input is changed!"));
$(document).observe("change", "#select", () =>
console.log("Select is changed!"));
$(document).observe("ajax", () =>
console.log("AJAX request completed."));
$(document).observe("ajax", "https://api.publicapis.org/random", () =>
console.log("AJAX request to /random completed."));
$("div#div")
.observe("added", "p", () => console.log("P added as a child of div#div!"))
.observe("removed", "p", () => console.log("P removed as a child of div#div!"));
});
$(() => {
setTimeout(() => $.get("https://api.publicapis.org/entries"), 2000);
setTimeout(() => $.get("https://api.publicapis.org/random"), 3000);
setTimeout(() => $("#div").append('<p id="p">Pp</p>'), 4000);
setTimeout(() => $("#div p").remove(), 6000);
});