How to bind an event to appended element in jQuery?

What is the performance difference between binding an onclick event through jQuery (1) and binding an onclick event through an HTML tag attribute (2)?

  • // Method 1 $('#element').click(function() { // code }); -vs- <!-- Method 2 --> <div class="x" id="element" onclick="handler();"></div>

  • Answer:

    The performance difference, if there is one, is negligibly small compared to the cost of tightly coupling your markup and behavior. Unless you have an extremely good reason, [1] keep the Javascript away from your HTML and attach handlers with jQuery or the native methods. Well-abstracted, maintainable code is almost always worth a few extra function calls. ---- [1] You don't.

Bulat Bochkariov at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.