DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
jQuery

Why use jQuery on() instead of click()

Share

The on() method attaches one or more event handler functions for one or more events to the selected elements.

Event handlers attached using the jQuery on() method will work for both current and future elements i.e. dynamic element created by a script.

Note: As of jQuery version 1.7, the jQuery on() method is the new replacement for the jQuery bind(), live() and delegate() methods.

To attach an event to the selected element that only runs once and then removes itself, you can use the one() method.

Syntax:
$(selector).on(event, childSelector, data, function, map)

So, why use jQuery on() instead of click()? Any idea?

Because you might have a dynamically generated elements (for example an AJAX call), you might want to have the same click handler that was previously bound to the same element selector You then “delegate” the click event using on() with selector argument.

Get more details from jQuery documentation.

Below example can describe the problem easily.

Suppose you have following HTML:

<ul id=”click”>

<li>List item with click</li>

</ul>

<ul id=”on”>

<li>List item with on</li>

</ul>

And this is the script:

$(‘#click li’).click(function() {

$(this).parent().append($(‘<li>List item with click</li>’));

});

$(‘#on’).on(‘click’, ‘li’, function() {

$(this).parent().append($(‘<li>List item with on</li>’));

});

So, in this case, if you click on “<li>” within id “on” then you can append “<li>” within that id. Again if you click on dynamic appended “<li>” then you can also append “<li>” within that id.

But, if you click on “<li>” within id “click” then you can append “<li>” within that id. Again if you click on dynamic appended “<li>” then you can not append “<li>” within that id as this is added in future. Only clicking on first “<li>”, you can append “<li>”.

Namaste UI (Author)

Namaste UI collaborates closely with clients to develop tailored guest posting strategies that align with their unique goals and target audiences. Their commitment to delivering high-quality, niche-specific content ensures that each guest post not only meets but exceeds the expectations of both clients and the hosting platforms. Connect with us on social media for the latest updates on guest posting trends, outreach strategies, and digital marketing tips. For any types of guest posting services, contact us on info[at]namasteui.com.

Recent Posts

6 Common Injuries After a Motorcycle Accident

Motorcycle accidents often lead to severe injuries because riders have minimal protection compared to drivers…

3 days ago

How Automation And SEO Can Improve Customer Experience

No business owner starts his or her business with the hope of making losses. Everyone…

3 days ago

5 Effects of a Car Accident

Car accidents can be traumatic, and the physical and psychological aftermath of a car accident…

5 days ago

How Worker’s compensation insurance Works?

Worker's compensation insurance deals with all those employees that get work-related injury or illness. When…

5 days ago

Why Mobile-First Stores Win: The Complete Guide to Mobile Commerce UX

Mobile commerce is no longer a trend. It is the main stage where buyers make…

5 days ago

What are the Benefits of Artificial Intelligence in Education?

The field of education welcomes and recognizes AI's potential to provide a more immersive learning…

5 days ago