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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:13:34+00:00 2026-06-10T18:13:34+00:00

I am really confused about each function. JsFiddle <html> <head> <script type=text/javascript src=jquery-1.8.0.min.js></script> <script>

  • 0

I am really confused about each function.

JsFiddle

<html>
    <head>
        <script type="text/javascript" src="jquery-1.8.0.min.js"></script>
        <script>
        $(document).ready(function(){
            //$(".a").click(function(){
            //  alert("hi");
            //});
            $(".a").each(function(){
                $(this).click(function(){
                    alert("hi");
                });
            });
        });
        </script>
    </head>
    <body>
    <div class="a">hi</div>
    <div class="a">hi</div>
    </body>
</html>

What’s the difference between using .click function directly and using .each? Both of them works.

Thanks a lot.

EDIT

If these two methods are the same, could you please give an example why .each is necessary?

  • 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-10T18:13:36+00:00Added an answer on June 10, 2026 at 6:13 pm

    Actually there is a difference here. With .each you create a new function in every iteration and each element gets a different function object assigned as event handler.
    This is not the case if you call .click on the whole selection. It will use the function you pass to it as event handler for each element. The behaviour in the end is the same though.

    You could avoid creating a new function object by creating it once before you iterate over the elements:

    var handler = function(){
        alert("hi");
    };
    $(".a").each(function(){
        $(this).click(handler);
    });
    

    But it is unnecessary to do so. .click calls .on internally which in turn will call .each to iterate over the selected elements and bind the event handler.

    .each is just a generic way to iterate over the elements and do whatever you want with them. For example, you might want to replace a word inside each element by another word:

    $('a').each(function() {
        $(this).text($(this).text().replace('foo', 'bar'));
    });
    

    This is not a very good example because also here there is a neater way to do it, but I hope it gives you some idea.


    Why is there a performance difference between these two ways?

    The outer .each call is redundant and adds an additional overhead. The “call chain” will look similar to this:

    each (1 time)
      -> callback (N times)
        -> click (1 time) (N times in total)
          -> on (1 time) (N times in total)
            -> each (1 time) (N times in total)
              -> callback (which ultimately adds the event handler) (1 time) (N times in total)
    

    In comparison, this is roughly what happens when you call .click directly:

    click (1 time)
      -> on (1 time)
        -> each (1 time)
          -> callback (which ultimately adds the event handler) (N times)
    

    So not only are more functions executed, but additional, the same functions (like .click nad .on) are executed multiple times. Each function might make checks or initialise variables once per call, so obviously if you call it multiple times, it will take more time.

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

Sidebar

Related Questions

I am really confused about object relationships! I have two classes Person and Address.
I'm really confused about the visitor pattern and its uses. I can't really seem
I am really confused about how I can do this. I need to get
I'm at a point where I'm really confused about how to go about handling
I'm new to the entity framework and I'm really confused about how savechanges works.
Well I'm new to PHP but thinking about these stuff has really confused me
I'm really confused about this, tried to read a lot about diagrams but I
I'm confused about python greedy/not-greedy characters. Given multi-line html, return the final tag on
I am really confused about target platform. For best results, to make my application
I've just learned about what std::function really is about and what it is used

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.