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

  • SEARCH
  • Home
  • 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 7082721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:05:22+00:00 2026-05-28T07:05:22+00:00

Maybe I misunderstood the functions of .on() api of jQuery . Here is the

  • 0

Maybe I misunderstood the functions of .on() api of jQuery. Here is the code I am trying out:

http://jsfiddle.net/tugberk/Um9VE/

<a class="btn" href="#">foo</a>
<a href="#" class="new">New</a>

<script>
    $(function(){ 
        $("a.btn").on("click", function(){ 
            alert("foo");
        });
        $("a.new").click(function(){
            $("html").append($("<a/>").attr("class", "btn").attr("href", "#").text("bar"));
        });
    });
</script>

When I create a new a.btn element, the event is not auto-attached after the element is being created. I can achieve this with .bind() api easily by triggering it after I create the new button but one of my friends suggested me that .on() event is doing this automatically.

So, did I misunderstand the capabilities of .on() api?

  • 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-05-28T07:05:23+00:00Added an answer on May 28, 2026 at 7:05 am

    For .on() to work like .delegate() or .live() it requires a special syntax. The different kinds of ways you can use .on()/.off() are shown below, together with the old ways of doing it with .bind()/.live() and .delegate(), along with .undelegate() and .die().

    $('a').bind('click', myHandler);
    $('a').on('click', myHandler);
    
    $('form').bind('submit', { val: 42 }, fn);
    $('form').on('submit', { val: 42 }, fn);
    
    $(window).unbind('scroll.myPlugin');
    $(window).off('scroll.myPlugin');
    
    $('.comment').delegate('a.add', 'click', addNew);
    $('.comment').on('click', 'a.add', addNew);
    
    $('.dialog').undelegate('a', 'click.myDlg');
    $('.dialog').off('click.myDlg', 'a');
    
    $('a').live('click', fn);
    $(document).on('click', 'a', fn);
    
    $('a').die('click');
    $(document).off('click', 'a');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe my title is not the best but here is what I really want.I
Maybe I just can't figure out the right keywords to get an answer out
Maybe I have misunderstood the point of the ActionFilterAttribute, but what I have now
So, maybe I misunderstood the usage of Func but Func<ComboBox, string> getCurrentValue = s
I have following code: class EntityBase (object) : __entity__ = None def __init__ (self)
Maybe I misunderstood the layout_weight parameter but I can't get it why something like
Maybe I misunderstood this but from what I read I may have several problems
Maybe I just don't know .NET well enough yet, but I have yet to
Maybe I misunderstood the semantics of get and merge in hibernate, but if I
I am new to mongodb and thinking of trying this out in PHP5. If

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.