Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:15:04+00:00 2026-06-09T08:15:04+00:00

Is there any difference between the following code? $(‘#whatever’).on(‘click’, function() { /* your code

  • 0

Is there any difference between the following code?

$('#whatever').on('click', function() {
     /* your code here */
});

and

$('#whatever').click(function() {
     /* your code here */
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:15:06+00:00Added an answer on June 9, 2026 at 8:15 am

    I think, the difference is in usage patterns.

    I would prefer .on over .click because the former can use less memory and work for dynamically added elements.

    Consider the following html:

    <html>
        <button id="add">Add new</button>
        <div id="container">
            <button class="alert">alert!</button>
        </div>
    </html>
    

    where we add new buttons via

    $("button#add").click(function() {
        var html = "<button class='alert'>Alert!</button>";
        $("button.alert:last").parent().append(html);
    });
    

    and want “Alert!” to show an alert. We can use either “click” or “on” for that.


    When we use click

    $("button.alert").click(function() {
        alert(1);
    });
    

    with the above, a separate handler gets created for every single element that matches the selector. That means

    1. many matching elements would create many identical handlers and thus increase memory footprint
    2. dynamically added items won’t have the handler – ie, in the above html the newly added “Alert!” buttons won’t work unless you rebind the handler.

    When we use .on

    $("div#container").on('click', 'button.alert', function() {
        alert(1);
    });
    

    with the above, a single handler for all elements that match your selector, including the ones created dynamically.


    …another reason to use .on

    As Adrien commented below, another reason to use .on is namespaced events.

    If you add a handler with .on("click", handler) you normally remove it with .off("click", handler) which will remove that very handler. Obviously this works only if you have a reference to the function, so what if you don’t ? You use namespaces:

    $("#element").on("click.someNamespace", function() { console.log("anonymous!"); });
    

    with unbinding via

    $("#element").off("click.someNamespace");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference between doing this: $(.topHorzNavLink).click(function() { var theHoverContainer = $(#hoverContainer); var
Is there any practical difference between the following two code snippets: NSObject * obj
Is there any difference between the following pieces of code in terms of processor
Is there any kind of difference between the following two lines of code in
Is there any execution speed difference between the following two lines of code? I
Is there any difference between following two ways of creating an object. Student s1
Is there any difference between the following two examples and should one be preferred
Question 1 Can anyone tell me if there is any difference between following 2
Is there any difference in efficiency (e.g. execution time, code size, etc.) between these
is there some significant difference between PHP 5.4 and 5.2.13 for the following code?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.