jQuery Events
jQuery events let you run code when users interact with the page, such as clicking a button, typing in a field or submitting a form. jQuery wraps the browser's event system in a short, consistent syntax built around the .on() method, and adds handy features like event delegation for elements that are created later.
Waiting for the DOM
Handlers can only be attached to elements that already exist. If your script loads in the head, wrap your code in the ready handler, written in jQuery 3 as $(function () { ... }). It runs once the HTML has been parsed. The older $(document).ready(fn) form still works, while $(document).on("ready", fn) was removed in jQuery 3. If your script is placed at the end of the body, the DOM is already there.
Binding events with .on()
Select elements, call .on() with an event name and a handler. Inside the handler, this is the DOM element that received the event; wrap it as $(this) to use jQuery methods on it. Shorthand methods such as .click(fn) and .submit(fn) exist too, but they are deprecated in recent jQuery versions, so .on() is the recommended form.
$(function () {
// Simple click
$("#toggle-menu").on("click", function () {
$("#menu").slideToggle(200);
});
// Several events, one handler
$("#search").on("focus blur", function (event) {
$(this).toggleClass("active", event.type === "focus");
});
// Form submit with preventDefault
$("#contact-form").on("submit", function (event) {
event.preventDefault();
const email = $("#email").val().trim();
$("#status").text(email ? "Thanks, we got it!" : "Please enter an email");
});
// Delegation: works for items added later
$("#todo-list").on("click", ".delete", function () {
$(this).closest("li").remove();
});
$("#add").on("click", function () {
$("#todo-list").append('<li>New task <button class="delete">x</button></li>');
});
});
Event delegation for dynamic content
A direct binding like $(".delete").on("click", fn) only attaches to elements that match at that moment. Buttons added afterwards get no handler, which is the most common jQuery event bug. Delegation solves it: attach the handler to a parent that always exists and pass a selector as the second argument. Events bubble up to the parent, and jQuery runs the handler only if the click started inside an element matching .delete. One delegated handler also uses less memory than hundreds of individual ones.
The event object
jQuery passes a normalised event object to every handler. Useful members include event.type, event.target (the element actually clicked), event.which or event.key for keyboard events, event.preventDefault() to block default actions like following a link, and event.stopPropagation() to stop bubbling. Returning false from a jQuery handler does both at once, which is convenient but can accidentally block other handlers.
Removing and triggering
.off("click") removes click handlers from the selected elements. Namespaced events such as "click.menu" let you remove only your own handlers with .off(".menu"), leaving those added by plugins alone. .one() attaches a handler that runs only once. .trigger("click") fires an event from code, which helps when one action should reuse another's logic.
A subtle duplicate-handler trap
If you call .on() inside another handler, for example binding a click every time a modal opens, the handlers stack up. After opening the modal three times, a single click runs the code three times. Bind once at start-up, use delegation, or call .off() before rebinding.
Takeaway: wrap code in $(function () { ... }), bind with .on(), use delegation for elements added later, read details from the event object, and avoid binding the same handler repeatedly.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.