KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Question How can a callback function retain a local variable from whence it was created? Simple example I'm creating a video player. It will have sliders to control the saturation, contrast, and hue. When the user plays with the sliders, it needs to acknowledge which slider got changed and what value it got changed to. The problem is that the name of the slider is a local variable from the scope of the creator of this onChange callback. How can this callback retain the name of the slider? HTML <div id="saturation"> <div class="track"></div> <div class="knob"></div> </div> </div> <div id="contrast"> <div class="track"></div> <div class="knob"></div> </div> </div> <div id="hue"> <div class="track"></div> <div class="knob"></div> </div> </div> JS var elements = [ 'saturation', 'contrast', 'gamma' ]; for(var i = 0; i < sliders.size(); i++) { new Control.Slider( $(elements[i]).down('.knob'), $(elements[i]).down('.track'), { onChange: function(value) { // ERROR: elements[i] is undefined alert(elements[i] + ' has been changed to ' + value); } } }
Tags (comma-separated)
Save Edits
Cancel