Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery .on() Method

Posted in jQuery Tutorial
Updated on Oct 13, 2024
By Mari Selvan
👁️ 63 - Views
⏳ 4 mins
💬 1 Comment
jQuery .on() Method

Photo Credit to CodeToFun

🙋 Introduction

jQuery is renowned for its simplicity and power in handling DOM manipulation and event handling. Among its plethora of methods, the .on() method stands out as a versatile tool for attaching event handlers to elements, including those dynamically created. Understanding and mastering this method can significantly streamline your event handling code and make your web applications more efficient.

In this guide, we'll explore the jQuery .on() method in detail, providing examples to illustrate its usage and benefits.

🧠 Understanding .on() Method

The .on() method in jQuery is used to attach one or more event handlers for specified elements, or to a selected set of elements that match the selector, now or in the future.

💡 Syntax

The syntax for the .on() method is straightforward:

syntax.js
Copied
Copy To Clipboard
$(selector).on(event, childSelector, data, handler);

Parameters:

  • event: A string containing one or more DOM event types, such as "click" or "submit".
  • childSelector (optional): A string containing a selector expression to match the child elements to which the event delegation should be attached.
  • data (optional): Any data that needs to be passed to the event handler.
  • handler: A function to execute when the event is triggered.

📝 Example

  1. Basic Usage:

    Attaching a click event handler to all paragraphs:

    example.js
    Copied
    Copy To Clipboard
    $("p").on("click", function() {
      alert("Paragraph clicked!");
    });
  2. Event Delegation:

    Using event delegation to attach a click event handler to all paragraphs inside a div with the ID container:

    example.js
    Copied
    Copy To Clipboard
    $("#container").on("click", "p", function() {
      alert("Paragraph inside container clicked!");
    });
  3. Handling Data with Event:

    Passing data to the event handler:

    example.js
    Copied
    Copy To Clipboard
    $("button").on("click", { foo: "bar" }, function(event) {
      alert("Data passed: " + event.data.foo);
    });
  4. Dynamic Elements:

    Attaching events to dynamically created elements:

    example.js
    Copied
    Copy To Clipboard
    $("body").on("click", "button", function() {
      alert("Button clicked!");
    });
  5. li>

    Unbinding Events:

    Removing event handlers using .off() method:

    example.js
    Copied
    Copy To Clipboard
    $("button").off("click");
    li>

    Namespaced Events:

    Using namespaced events for better organization and unbinding:

    example.js
    Copied
    Copy To Clipboard
    $("button").on("click.myNamespace", function() {
      alert("Button clicked!");
    });

    To unbind just this specific event, use:

    example.js
    Copied
    Copy To Clipboard
    $("button").off("click.myNamespace");

🎉 Conclusion

The jQuery .on() method is a powerful tool for event handling in web development. Whether you need to attach event handlers to existing elements, handle events on dynamically created elements, delegate events, or pass data to event handlers, .on() provides a flexible and efficient solution.

By mastering its usage and understanding its nuances, you can write cleaner, more organized, and more maintainable code for your web applications.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy