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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:57:14+00:00 2026-06-03T04:57:14+00:00

I’ve been doing this: $(‘#id’).on(‘click’,function(e){myFunction(var1,var2);}); function myFunction(x,y){blah blah blah} I’m wanting to change to

  • 0

I’ve been doing this:

$('#id').on('click',function(e){myFunction(var1,var2);});

function myFunction(x,y){blah blah blah}

I’m wanting to change to this:

$('#id').on('click',function(e){blah blah blah});

If I do this with lots of different DOM elements, am I consuming more memory space because I’m multiplying the space taken up by myFunction with each closure? Or does it just use a pointer?

  • 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-03T04:57:15+00:00Added an answer on June 3, 2026 at 4:57 am

    The first code has the advantage of reuse, but has the disadvantage of defining an anonymous function just to call the real function. If it really matters, then the extra function is an overhead.

    The second one will take much less memory since you are passing only one function. Functions are also passed by reference. Meaning this function could be stored once and called by many different callers.


    However, the way you attach it also affects performance. If you do it like this:

    $('#id1').on('click',function(e){myFunction(var1,var2);});
    $('#id2').on('click',function(e){myFunction(var1,var2);});
    

    this one calls jQuery 4 times (2 $() and 2 on()), creates 2 anonymous functions and calls an external function twice. count: 6 calls, 3 functions just to execute your operation (not including internal jQuery calls).

    $('#id1').on('click',function(e){blah blah blah});
    $('#id2').on('click',function(e){blah blah blah});
    

    if you do it this way, the function isn’t reusable anymore but you got 4 calls to jQuery, 2 functions but you are repeating the code. not good from a DRY perspective.

    $('#id1, #id2').on('click',function(e){blah blah blah});
    

    this one, you have 2 calls, and 1 function. this one is better.

    $('#id1, #id2').on('click',function(e){myFunction(var1,var2);});
    

    you could also do it this way, the function is reusable with a sacrifice of an additional anonymous function and call. count: 3 calls, 2 functions.


    You could harness the power of .on by attaching the handler to the “nearest common parent”, nearest so that the bubble won’t travel up very far. in comparison, live() attaches events to the document, making handlers available even to new elements. But attaching to the document makes it very far for deeply nested elements.

    $('nearest_common_parent').on('click','dynamic_element',function(){
        //do stuff
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

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.