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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:16:48+00:00 2026-06-05T21:16:48+00:00

Whilst reading jQuery Cookbook (Oreilly) last night I came across an each function that

  • 0

Whilst reading jQuery Cookbook (Oreilly) last night I came across an each function that produces a question that I can’t seem to find an answer in the book for, or online.
The code I use for this question is found from jQuery site and I included it below as a reference:

<script>
    $(document.body).click(function () {
      $("div").each(function (i) {            //Where does the 'i' come from?
        if (this.style.color != "blue") {
          this.style.color = "blue";
        } else {
          this.style.color = "";
        }
      });
    });
</script>

I would like to know the origin and purpose of the ‘i’ parameter as I fail to see where it comes from (client code) and what it is used for? As a Java guy I will grasp the concept a lot easier as I familiar with method or ‘function’ params in the context of Java.

Here I don’t see the client code (I presume it’s in the library) and I don’t see how it (the i) is relevant in the function as it’s not referenced.

Can someone from the community give a clear explanation for this or refer me to a guide on this?

I understand the purpose of the each function and the ‘this’ reference so you don’t need to explain these unless you feel it relevant to future viewers of this question.

  • 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-05T21:16:50+00:00Added an answer on June 5, 2026 at 9:16 pm

    In that case, there was no need to declare the i. The signature of the each method is stated in the jQuery docs:

    .each( function(index, Element) )

    As you can see, it takes one argument, and that argument is a function that takes 2 arguments, index and Element.

    In your code, i is an identifier for index, which is passed to the callback function by jQuery each time it is called (once for each iteration). It also passes a second argument, but you have not declared an identifier for it so it will only be accessible through the arguments object.

    You can see exactly what is passed to the callback by logging that arguments object:

    ​$("div").each(function () { //No arguments listed in callback signature...
        console.log(arguments); //But arguments are still passed
    });​​
    

    Here’s a working example of the above.

    Here’s the relevant code from jQuery itself (comments added):

    //...
    each: function( object, callback, args ) {
        //...
    
        //Iterate over the matched set of elements
        for ( ; i < length; ) {
            /* Call the callback in the context of the element, passing in the 
               index and the element */
            if ( callback.call( object[ i ], i, object[ i++ ] ) === false ) {
                break;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whilst reading through K&R, I came across the integer to string function. I gave
Whilst reading through the book The well grounded Rubyist, I came across some strange
I came across this code today whilst reading Accelerated GWT (Gupta) - page 151
Whilst reading through the DirectWrite source code I came across the following struct: ///
Whilst working on some generally horrible Javascript code this morning, I came across the
The StreamReader locks a text file whilst it is reading it. Can I force
Whilst trawling through some old code I came across something similar to the following:
I have been reading this question and a few other answers and whilst I
Whilst reviewing some Qt C++ code I came across this: class Foo { Q_OBJECT
Whilst inside an iframe, can you please let me know how I can obtain

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.