0

I'm having trouble getting the value of i to be passed on to the scMotion function

for ( i = 0; i < tmp.length; i++ ) {
document.getElementById("sc"+i).onmousedown=function() { return scMotion(i,'up') };
}

To clarify the question, this for loop is doing other stuff, adding elements to the dom.

For some reason even if i is at number 39 the value of i being passed in the function I ma attaching is the final value of i i.e 80.

6
  • i is in different scope. That's the problem. Commented Nov 23, 2012 at 11:26
  • 2
    @xyu: perhaps you need to refresh your knowledge about variable scopes in JS and about closures especially. Commented Nov 23, 2012 at 11:27
  • I know it's in a different scope so how get it in the right scope, that's the question. Commented Nov 23, 2012 at 11:27
  • @user1209203: there is nothing wrong with scope. Can you explain the "trouble"? Commented Nov 23, 2012 at 11:28
  • var At the beginning of declaring the variable makes it accessible. The different scope shouldn't matter. Commented Nov 23, 2012 at 11:28

2 Answers 2

6

That works as you typed it because there should be a closure between i and the function:

var i="4";
document.getElementById("sc"+i).onmousedown=function() {
    return scMotion(i,'up'); // <-- due to the created closure, the i here
                             //     refers to the i above.
};

However, remember that it's a closure. So perhaps the following scenario is what's tripping you up:

var i="4";
document.getElementById("sc"+i).onmousedown=function() {
    return scMotion(i,'up'); // <-- due to the created closure, the i here
                             //     refers to the i above.
                             //     However, when the event happens in the
                             //     future the value of i in here will
                             //     be "5" due to the code below:
};
i = "5";

This is a classic closure in a loop problem. See this to understand what's happening: Please explain the use of JavaScript closures in loops

Sign up to request clarification or add additional context in comments.

1 Comment

That's the one I had come across this before but couldn't remember exactly the solution.
0

Try instead to use addEventListener. You can use the id to pass the number to your function.

for ( i = 0; i < tmp.length; i++ ) {

    var el = document.getElementById( "sc" + i );

    if ( el.addEventListener ) {

        el.addEventListener( 'mousedown', handleMouseDown ); 

    } else if ( el.attachEvent )  { // for IE8 and previous versions

        el.attachEvent( 'mousedown', handleMouseDown );

    }
}

function handleMouseDown( event ) {

    var num = event.target.id.replace( "sc", "" );
    scMotion( num, 'up' );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.