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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:30:36+00:00 2026-05-10T21:30:36+00:00

I have a three-step process that is entirely reliant upon JavaScript and Ajax to

  • 0

I have a three-step process that is entirely reliant upon JavaScript and Ajax to load data and animate the process from one step to the next. To further complicate matters, the transition (forward and backward) between steps is animated :-(. As user’s progress through the process anchor’s appear showing the current step and previous steps. If they click on a previous step, then it takes them back to the previous step.

Right now, the entire process (forward and backward) works correctly, if you begin at step 1, but if you jump straight to step 3 then the anchors for step 1 and step 2 also perform the same action as step 3.

This is the portion of the code that loops through all of the steps up to the current step that the user would be on and displays each anchor in turn and assigns the appropriate function to the click event:

for (var i = 0; i < profile.current + 1; i++) {     if ($('step_anchor_' + i).innerHTML.empty()) {         var action = profile.steps[i].action;         var dao_id = profile.steps[i].dao_id;          $('step_anchor_' + i).innerHTML = profile.steps[i].anchor;         $('step_anchor_' + i).observe('click', function(){             pm.loadData(action, dao_id, true);         });          Effect.Appear('step_anchor_' + i, {             duration: 1,             delay: (down_delay++)         });     } } 

I know that problem lies in the way that the action and dao_id parameters are being passed in. I’ve also tried passing profile.steps[i].action and profile.steps[i].dao_id but in that case both profile and i or at least i are out scope.

How do I make it so that I can assign the parameters for action and dao_id correctly for each step? (If it makes any difference we are using Prototype and Scriptaculous)

  • 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. 2026-05-10T21:30:36+00:00Added an answer on May 10, 2026 at 9:30 pm

    Your closure scope chain is causing your problems. By declaring the handler function inline, you’ve created a closure. Obviously you did this to take advantage of the loop.

    However, since you have created a closure, you’re playing by closure scoping rules. Those rules state that the local variables within the parent function remain active and available as long as the closure exists.

    You are trying to pass and then use ‘action’ and ‘dao_id’ to your closure, but you are passing references here, not values. So when your closures (handlers) are called they use the value that the reference was last assigned. In your case, the Step 3 handler.

    Closure scoping rules are confusing enough, but you may also be confused by the fact that ‘action’ and ‘dao_id’ are still alive even though the loop block has finished executing. Well, in JavaScript there is no such thing as block scope. Once you declare a variable it is available until the end of the function or until is it deleted. Whichever comes first.

    All that said, you need to break the scope chain. Here are two ways to do that:

    Try this:

    for (var i = 0; i < profile.current + 1; i++) {     if ($('step_anchor_' + i).innerHTML.empty()) {         var action = profile.steps[i].action;         var dao_id = profile.steps[i].dao_id;          $('step_anchor_' + i).innerHTML = profile.steps[i].anchor;         $('step_anchor_' + i).observe('click', function(a, b){                 return function(){pm.loadData(a, b, true)};         }(action, dao_id));          Effect.Appear('step_anchor_' + i, {                 duration: 1,                 delay: (down_delay++)         });     } } 

    Or this:

    function createHandler(action, dao_id) {     return function(){pm.loadData(action, dao_id, true);}; }   /* snip - inside some other function */ for (var i = 0; i < profile.current + 1; i++) {     if ($('step_anchor_' + i).innerHTML.empty()) {         var action = profile.steps[i].action;         var dao_id = profile.steps[i].dao_id;          $('step_anchor_' + i).innerHTML = profile.steps[i].anchor;         $('step_anchor_' + i).observe('click', createHandler(action, dao_id));         Effect.Appear('step_anchor_' + i, {                 duration: 1,                 delay: (down_delay++)         });     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 169k
  • Answers 169k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have you checked out the Async.BuildPrimitive function? I think you… May 12, 2026 at 1:55 pm
  • Editorial Team
    Editorial Team added an answer The problem was that ModelState was holding the dropdownlist's passed… May 12, 2026 at 1:55 pm
  • Editorial Team
    Editorial Team added an answer A ListViewItem can't belong to more than one ListView at… May 12, 2026 at 1:55 pm

Related Questions

I have a List of a complex type - an object with a few
I'm writing an algorithm in PHP to solve a given Sudoku puzzle. I've set
I have an abstract class in a library. I'm trying to make it as
I want a permission that will prevent people from logging in. (So, all users

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.