Home » Tutorial: jQuery Events

Tutorial: jQuery Events

jQuery makes event handling in web development straightforward, enabling you to respond to user interactions, page events, and other activities seamlessly.

This tutorial covers the most common jQuery event methods with detailed explanations and examples.

Table of Contents

1. What Are jQuery Events?

jQuery events are methods that allow developers to attach functionality to HTML elements when specific actions occur, such as clicks, hovers, typing, or form submissions.

2. Common jQuery Event Methods

Here’s a quick overview of the most commonly used event methods:

  • .click() – Triggered when an element is clicked.
  • .hover() – Detects when the mouse enters or leaves an element.
  • .on() – Attaches one or more event handlers to elements (preferred method).
  • .off() – Removes event handlers.

3. Mouse Events

Mouse events occur when users interact with the mouse.

Example 3.1: Click Event

<!DOCTYPE html>
<html>
<head>
    <title>jQuery Click Event</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#btn").click(function() {
                alert("Button clicked!");
            });
        });
    </script>
</head>
<body>
    <button id="btn">Click Me</button>
</body>
</html>

Example 3.2: Hover Event

<div id="hoverDiv" style="width:100px; height:100px; background-color:gray;"></div>

<script>
    $(document).ready(function() {
        $("#hoverDiv").hover(
            function() {
                $(this).css("background-color", "green");
            },
            function() {
                $(this).css("background-color", "gray");
            }
        );
    });
</script>

Example 3.3: Mouse Events (Mouseover and Mouseout)

$("#box").mouseover(function() {
    $(this).text("Mouse Over!");
}).mouseout(function() {
    $(this).text("Mouse Out!");
});

4. Keyboard Events

Keyboard events capture user input from the keyboard.

Example 4.1: Keypress Event

<input type="text" id="inputBox" placeholder="Type something...">
<p id="displayText"></p>

<script>
    $(document).ready(function() {
        $("#inputBox").keypress(function(event) {
            $("#displayText").text("Key pressed: " + event.key);
        });
    });
</script>

Example 4.2: Keydown and Keyup

$("#inputBox").keydown(function() {
    console.log("Key is being pressed");
}).keyup(function() {
    console.log("Key released");
});

5. Form Events

Form events deal with form elements such as inputs, checkboxes, and submissions.

Example 5.1: Focus and Blur Events

<input type="text" id="name" placeholder="Enter your name">

<script>
    $(document).ready(function() {
        $("#name").focus(function() {
            $(this).css("background-color", "#e0f7fa");
        }).blur(function() {
            $(this).css("background-color", "");
        });
    });
</script>

Example 5.2: Change Event

$("#dropdown").change(function() {
    alert("Selected value: " + $(this).val());
});

Example 5.3: Submit Event

<form id="myForm">
    <input type="text" placeholder="Enter something" required>
    <button type="submit">Submit</button>
</form>

<script>
    $(document).ready(function() {
        $("#myForm").submit(function(event) {
            event.preventDefault();
            alert("Form submitted!");
        });
    });
</script>

6. Window and Document Events

These events occur at the window or document level.

Example 6.1: Document Ready

$(document).ready(function() {
    console.log("Document is ready!");
});

Example 6.2: Window Resize

$(window).resize(function() {
    console.log("Window resized to: " + $(window).width() + "x" + $(window).height());
});

Example 6.3: Scroll Event

$(window).scroll(function() {
    console.log("Scrolled to: " + $(window).scrollTop());
});

7. Event Delegation

Event delegation is attaching events to a parent element to handle current or future child elements.

Example 7.1: Using .on() for Dynamic Elements

<ul id="list">
    <li>Item 1</li>
    <li>Item 2</li>
</ul>
<button id="addItem">Add Item</button>

<script>
    $(document).ready(function() {
        $("#list").on("click", "li", function() {
            alert($(this).text());
        });

        $("#addItem").click(function() {
            $("#list").append("<li>New Item</li>");
        });
    });
</script>

Example 7.2: Removing Event Handlers

$("#stop").click(function() {
    $("#list").off("click", "li");
});

Conclusion

jQuery provides a rich set of methods to handle events efficiently. Whether you need to respond to mouse clicks, keyboard input, or form submissions, jQuery's intuitive syntax simplifies the process.

Try out the examples and experiment with combining different events for more interactive web pages. For advanced usage, consult the official jQuery Event Documentation.

You may also like