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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:57:40+00:00 2026-05-26T02:57:40+00:00

Alright so, I have something that looks like this : for (j = 0;

  • 0

Alright so,

I have something that looks like this :

for (j = 0; j < btnArr.length; j++)
{
    var btn = document.createElement("button");
    btn.addEventListener("click", function() { press(this, j) }, false);
    div.appendChild(btn);
}

My problem is, since that the event happens at a late time, j has already changed value by time the event’s function is triggered. I want the function to directly use j‘s value when the function is declared, and not a reference to j itself.

If addEventListener‘s 2nd parameter was a string, it would look like this:

btn.addEventListener("click", "function() { press(this, " + j + ") }", false);

Does anyone know if it’s possible, and how to do this?

I tried searching around a bit, but I can’t find anything relevant, since the problem is hard to describe properly with only a few words.

By the way, this is for a Greasemonkey script, hence the use of .addEventListener() instead of .onclick = (...)

  • 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-05-26T02:57:41+00:00Added an answer on May 26, 2026 at 2:57 am

    You just need to break the context. It can be done a ton of ways, but Function.bind is the most elegant IMHO. Since you are in Firefox, bind should be there.

    for (j = 0; j < btnArr.length; j++)
    {
        var btn = document.createElement("button");
        btn.addEventListener("click", function(value) { press(this, value) }.bind(btn, j), false);
        div.appendChild(btn);
    }
    

    For an alternate approach, write a function that returns the handler.

    for (j = 0; j < btnArr.length; j++)
    {
        var btn = document.createElement("button");
        btn.addEventListener("click", getHandler(j), false);
        div.appendChild(btn);
    }
    
    function getHandler(j) {
        return function() { press(this, j) };
    }
    

    Yet another option is to add a property to the button element.

    for (j = 0; j < btnArr.length; j++)
    {
        var btn = document.createElement("button");
        btn.myValue = j;
        btn.addEventListener("click", function() { press(this, this.myValue); }, false);
        div.appendChild(btn);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright I have an xml document that looks something like this: <xml> <list> <partner>
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(
I have a view that I'm loading, and my onCreate() method looks like this:
I use a pattern that looks something like this often. I'm wondering if this
Alright, so this is something really basic, and I know that when someone tells
I am working with a legacy database schema that looks like this: product_table table
Alright so I have this C++ image capturing class. I was wondering if I
Alright, currently I have my SWF hitting a php file that will go and
Alright so this is a bizarre cross platform thing that I'm experiencing with text

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.