Basics Lesson 4 of 10
jQuery Events — on, click, input and Event Delegation
Handle events in jQuery with .on() — click, dblclick, input, change, submit, keyup and hover — use the event object, prevent default actions, and delegate events for dynamic elements.
Events make pages interactive. jQuery's .on() method attaches a function to an event on every selected element.
.on()
<script src="/vendor/jquery.min.js"></script>
<button id="like">👍 Like</button> <span id="likes">0</span>
<p><input id="name" placeholder="Type your name"> <span id="hello"></span></p>
<p>
<select id="strand"><option>STEM</option><option>ABM</option><option>HUMSS</option></select>
<span id="chosen"></span>
</p>
<p id="hover" style="padding:8px;border:1px dashed #999">Hover over me</p>
<script>
let likes = 0;
$("#like").on("click", function () {
likes++;
$("#likes").text(likes);
});
$("#name").on("input", function () {
$("#hello").text($(this).val() ? "Hi, " + $(this).val() + "!" : "");
});
$("#strand").on("change", function () {
$("#chosen").text("You chose " + $(this).val());
});
$("#hover").on("mouseenter", function () { $(this).css("background", "#fdf3dc"); })
.on("mouseleave", function () { $(this).css("background", ""); });
</script>
| Event | Fires when |
|---|---|
click, dblclick | Clicked / double-clicked |
input | Every change while typing |
change | A select, checkbox or finished input changes |
submit | A form is submitted |
keyup, keydown | A key is released / pressed |
mouseenter, mouseleave | Pointer enters / leaves |
focus, blur | An input gains / loses focus |
Shortcut methods like .click(fn) also work, but .on("click", fn) is the modern, recommended form.
The event object and preventDefault
<script src="/vendor/jquery.min.js"></script>
<form id="f">
<input id="qty" type="number" placeholder="Quantity" min="1">
<button>Compute</button>
</form>
<p id="out"></p>
<script>
$("#f").on("submit", function (e) {
e.preventDefault(); // don't reload the page
const qty = Number($("#qty").val());
$("#out").text(qty > 0 ? "Total: ₱" + (qty * 2.5).toFixed(2) : "Enter a quantity.");
});
</script>
The function receives an event object e. e.preventDefault() stops the browser's default action, and e.key (for keyboard events) tells you which key was pressed.
Event delegation
Handlers attached with $(".delete").on("click", ...) only apply to elements that exist at that moment. For items added later, attach one handler to a parent and give a selector as the second argument:
<script src="/vendor/jquery.min.js"></script>
<input id="task" placeholder="New task"> <button id="add">Add</button>
<ul id="list"><li>Review for quiz <button class="del">✕</button></li></ul>
<script>
$("#add").on("click", function () {
const text = $("#task").val().trim();
if (!text) return;
$("#list").append($("<li>").text(text + " ").append('<button class="del">✕</button>'));
$("#task").val("");
});
// One handler on the list works for current AND future .del buttons
$("#list").on("click", ".del", function () {
$(this).parent().remove();
});
</script>
Removing handlers and running once
$("#btn").off("click"); // remove click handlers
$("#btn").one("click", function () { // runs only the first time
alert("Welcome!");
});
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04