jQuery Events

jQuery events let you run a function when something happens in the browser, such as a click, a key press, focus entering a field, or a window resize.

For current jQuery code, the most flexible pattern is .on(). It can bind one event, several events, or delegated events for elements that are added later.

Basic Event Syntax Top ↑

$( "#b1" ).on( "click", function (event) {
    $( "#result" ).text( "Button clicked" );
});

The handler receives an event object. Useful properties include event.type, event.target, keyboard properties such as event.key, and pointer coordinates such as event.pageX.

jQuery Event Tutorials Top ↑

Event topicWhat you will learn
ClickRun code when an element is clicked.
Double ClickHandle a double-click separately from a normal click.
HoverRespond when the pointer enters and leaves an element.
Mouse Enter / LeaveUse mouseenter and mouseleave for element boundaries.
Mouse MoveRun code continuously as the pointer moves over an element.
Pointer CoordinatesRead the pointer position from an event object.
FocusReact when a form control receives focus.
BlurReact when a form control loses focus.
KeydownHandle a key being pressed down.
KeyupHandle a key being released.
KeypressUnderstand the legacy keypress event and its limitations.
Keyboard Key DetailsRead modern keyboard event properties instead of relying on the old jQuery event.which shim.
Keydown vs KeypressCompare keydown and keypress behavior.
Key Hold DurationMeasure how long a key is held.
Window ResizeReact when the browser window changes size.
ScrollReact to document or element scrolling.
Event Calendar ApplicationApplication example combining events, datepicker and AJAX.

Mouse Events with jQuery

Keyboard Events with jQuery

Form Component Events with jQuery

Window Events with jQuery

Bind More Than One Event Top ↑

Separate event names with spaces when the same handler should run for more than one event. The original page used .bind(); .on() is the current equivalent.

$( "#id_dtl" ).on( "blur keyup", function (event) {
    // Runs after either blur or keyup.
});

Identify Which Event Ran Top ↑

$( "#b1" ).on( "click dblclick", function (event) {
    $( "#t1" ).text( "Event type: " + event.type );
});

Delegated Events for Dynamic Elements Top ↑

If an element may be created after the page loads, bind the event to a stable ancestor and provide a selector.

$( "#display_body" ).on( "click", ".edit_class", function () {
    const id = this.id;
    console.log( "Clicked:", id );
});

Practice Ideas Top ↑

  1. Display a welcome message when a button is clicked, then remove it after two seconds.
  2. Use one handler for a group of radio buttons and display the selected value.
  3. Handle both click and double-click and display event.type.
  4. Create a live textarea character counter.
  5. Measure how long a key is held between keydown and keyup.

Start with the click event →






plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer