$("#canvas").mousedown(function(e) { for(i=0; i<allPoints[0].length; i++) { var pointX = controlPoint[i].x; var pointY = controlPoint[i].y; var mouseX = e.pageX - this.offsetLeft; var mouseY = e.pageY - this.offsetTop; if(pointX + 5 > mouseX && pointX - 5 < mouseX && pointY + 5 > mouseY && pointY - 5 < mouseY) { var testest = i; $(this).bind('mousemove', function(e) { //'i' is undefined here //'testest' is defined controlPoint[testest].x = e.pageX - this.offsetLeft; controlPoint[testest].y = e.pageY - this.offsetTop; }); } } }).mouseup(function(e){ $(this).unbind('mousemove'); }); For some reason when I try to use the variable 'i' from the for loop inside of my mousemove function it is undefined. If I assign it to another variable before that works for some reason, but I don't want to do that because it seems unnecessary. Any help would be greatly appreciated.