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 8927751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:17:02+00:00 2026-06-15T08:17:02+00:00

Time ago, I used to use this solution: $(‘<div class=ok />’) .appendTo(‘body’); $(‘div.ok’) .live(‘click’,function(){

  • 0

Time ago, I used to use this solution:

$('<div class="ok" />')
.appendTo('body');

$('div.ok')
.live('click',function(){ alert(); })

Now I use this one:

$('<div class="ok" />')
.appendTo('body')
.click(function(){ alert(); })

How is performance difference? I believe the second solution is better because It doesn’t require a live() .
Is it always this way (also with many elements) or not? why?

  • 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-15T08:17:03+00:00Added an answer on June 15, 2026 at 8:17 am

    How is performance difference?

    Well, when you used to use live. live always bound to the document and preventing events from bubbling up was impossible too as the event was triggered after it already bubbled all the way through.

    In that sense live() was not very performance friendly.

    Your second code sample binds to the object directly as it is created, making it perform better and more flexible when compared to live(). You can now event prevent the event from bubbling up and so on.

    Binding the event at creation to the element directly compared to using on(), specifying the closest static element to bind to has only a very small performance benefit.

    // Performs slightly better than on() but cannot be executed from anywhere unless the element is added at the same time.
    $('<div class="ok" />').appendTo('body').click(function(){ alert(); })
    

    The benefit is smaller though than the benefit of being able to bind your event handlers before the element is added in a common method for example.

    The below does the same as your second code sample but can be nicely executed within a common method and does not have to be part of adding the dynamic element.

    // Can be called in a common init method for example and still work.
    $(body).on('click', '.ok', function(){ alert();})
    

    on() has the great benefit that you can separate the adding of elements from the binding of events.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've used ITK with c++ some time ago and I'm trying to use simple
On a website ( http://webdesign.onyou.ch/2010/08/04/javascript-time-ago-pretty-date/ ) I found this very useful function, you give
I found this solution for adding a link to a pdf some time ago.
A long time ago I used to use or die in my PHP code.
I havent used lucene. Last time i ask (many months ago, maybe a year)
some time ago I happend to resolve a PG related problem with this SO
Some time ago I created this LAMP based web. At the time I wrote
Either i'm going nuts or nobody likes/liked this feature, but a long time ago
Some time ago I had used SCTP on Linux and now I want to
I used to use this when I wanted to trigger errors in PHP, coming

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.