jQuery Events

click, hover, keyup, on and more.

✍️ 3 exercises + code challenge in this lesson

Attach event handlers with shortcut methods or .on():

  • .click(), .dblclick(), .hover()
  • .keyup(), .change(), .submit(), .focus()
  • .on("click", handler): the general form, which also supports several events at once.
  • Delegation: $("#list").on("click", "li", handler) works for items added later.

Key Takeaways: jQuery Events

Mastering jQuery Events helps you write clean and reliable jQuery code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

jQuery Events Code Example

jQuery
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<input id="name" placeholder="Type...">
<p>Preview: <strong id="preview"></strong></p>
<div id="box" style="width:120px;height:60px;background:#0769AD;color:white;padding:6px">Hover me</div>

<script>
  $("#name").on("keyup", function () {
    $("#preview").text($(this).val());
  });

  $("#box").hover(
    function () { $(this).css("background", "#14B8A6").text("Hovering!"); },
    function () { $(this).css("background", "#0769AD").text("Hover me"); }
  );
</script>
Free sign-in required · Google login available

jQuery Events Practice Exercises

Check what you learned in “jQuery Events”.

/ 3 solved
Fill in the blank

Attach a click handler.

$("#btn").("click", function () { });
✓ Correct! Not quite. Try again.
Multiple choice

Which technique handles events for elements added later?

✓ Correct! Not quite. Try again.
Code challenge

When #btn is clicked, set the text of #msg to "Clicked". (The check clicks it.)

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services