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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:46:24+00:00 2026-05-21T10:46:24+00:00

I have the following function. The problem is that instead of waiting for the

  • 0

I have the following function. The problem is that instead of waiting for the user to click the image as expected, the function immediately fires the imgReplace function for each element in the images array.

Have I done something wrong?
Could the fact I’m using a separate Javascript routine based on Jquery be relevant here?

function setup () {
    var images = document.getElementById("mycarousel");
    images = images.getElementsByTagName("img");

    for (var i = 0; i< images.length; i++) {
        images[i].onclick = imgReplace (images[i]);
    }

}
  • 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-21T10:46:25+00:00Added an answer on May 21, 2026 at 10:46 am

    Wow I just fixed this embarrassing bug in some of my own code. Everybody else has gotten it wrong:

    images[i].onclick = function() {imgReplace(images[i]);};
    

    won’t work. Instead, it should be:

    images[i].onclick = (function(i) { return function() { imgReplace(images[i]); }; })(i);
    

    Paul Alexander’s answer is on the right track, but you can’t fix the problem by introducing another local variable like that. JavaScript blocks (like the {} block in the “for” loop) don’t create new scopes, which is a significant (and non-obvious) difference from Java or C++. Only functions create scope (setting aside some new ES5 features), so that’s why another function is introduced above. The “i” variable from the loop is passed in as a parameter to an anonymous function. That function returns the actual event handler function, but now the “i” it references will be the distinct parameter of the outer function’s scope. Each loop iteration will therefore create a new scope devoted to that single value of “i”.

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

Sidebar

Related Questions

I have the following function. The problem is that the $lang variable will vary
Never ran into this problem with jQuery before. I have the following: $(document).ready(function() {
I have problems with following code: http://lisper.ru/apps/format/96 The problem is in normalize function, which
In my home work of Ackermann function I have solved the problem as following
The problem is the following. We have lots of ajax call via $.ajax function.
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
I have the following code in my codebehind Page_Load function that sets the default
I have a continuous linear programming problem that involves maximizing a linear function over
I have the following code which works well, but the problem is that after
I have the following jQuery code: $content.each(function(i) { $body = $(this); $(this).find('div.field-content') .animate( {

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.