๐Ÿ’ฒ jQuery Essentials

jQuery Events: Click, Hover, Submit & Delegation Explained

By The CodeCraft Teamยทยท7 min read
jQuery .on() event delegation diagram

jQuery's event API is friendly, consistent across browsers, and great for handling dynamic content. Here's everything you need to handle events confidently.

Click and hover

$("#btn").on("click", function () {
  $(this).toggleClass("active");
});

$(".card").hover(
  () => $(this).addClass("hover"),
  () => $(this).removeClass("hover")
);

Form submit

$("form").on("submit", function (e) {
  e.preventDefault();
  const data = $(this).serialize();
  console.log(data);
});

Event delegation (critical for dynamic content)

// Works even for items added later
$("#list").on("click", ".item", function () {
  $(this).remove();
});

Frequently asked questions

Why use event delegation?

It attaches one listener to a parent instead of many to children โ€” faster and works for dynamically added elements.

External references

Enjoyed this article?

Share it with a fellow developer or explore more tutorials in our blog.

More articles

Related articles