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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:49:29+00:00 2026-06-14T08:49:29+00:00

The variable myCollection holds jQuery DOM elements, and I want to set a callback

  • 0

The variable myCollection holds jQuery DOM elements, and I want to set a callback function for each of their onclick event as shows below. Actually what I want is, to incrementally set or could say “burn” the number for each element, so when they are getting clicked, its own number would get alerted. This code alerts the last value of number when it was incremented in the loop the last time.

var number = 1;

myCollection.each(function(){
    this.onclick = function() { someFunction(number) };
    number++;
});

//This function is somewhere else, but accessible.
function someFunction(number) {
    alert(number);
}

How can I copy or instantiate callback functions for each elements, so they would have the same process but with different arguments bind to them?

  • 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-14T08:49:31+00:00Added an answer on June 14, 2026 at 8:49 am

    A classic scoping problem, it can be solved like this:

    myCollection.each(function(){
      this.onclick=(function(number) {
          // in this scope, number has been "burned"
          return function() {
              someFunction(number)
          };
      }(number));
      number++;
    });
    

    Note that jQuery already includes a correctly scoped index inside the each callback, so all you need to do is:

    myCollection.each(function(i){
        $(this).click(function(){
            someFunction(i+1);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a js variable with many functions, and I want to create in
I have bitmap image variable and i want to bind it to my xaml
I have a JSON variable defined as: var myCollection = { data: [ {
Variable $d comes from file_get_contents function to a URL. $answer = @new SimpleXMLElement($d); Below
Variable in my model [Display(Name = Enlem)] public double Latitute { get; set; }
Variable names in haskell need to be in small case, but How to declare
Variable outside of the loop int number = 0; for(int i = 0; i
the variable doc is not accessible anywhere else in the method outside of the
The variable filepath which is a string contains the value Música . I have
The variable cherrypy.request.params as it is described in the API contains the query 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.