I'm using noUiSlider (http://refreshless.com/nouislider/) on a form which will have dozens of sliders on it. Rather than copy/pasting the code for each, I thought I could just set up an array of class names and a loop. That works; ie, it sets up working sliders. However the form also has to show the value of a slider upon update, and that's the part that I can't work out. I know how to do it with a static value but not in the loop ...
Simplified example:
JavaScript:
var steps = [
'Never',
'Occasionally',
'Frequently',
'Constant'
];
// This array will have many more
var slider_names = [
'slider',
'slider2'
];
var sliders = [];
for (var i = 0; i < slider_names.length; i++) {
sliders.push(document.getElementById(slider_names[i]));
noUiSlider.create(sliders[i], {
start: 0,
connect: 'lower',
step: 1,
range: {
'min': [ 0 ],
'max': [ 3 ]
},
pips: {
mode: 'steps',
density: 100
}
});
sliders[i].noUiSlider.on('update', function(values, handle) {
// ***** Problem line *****
document.getElementById(slider_names[i]+'-value').innerHTML = steps[parseInt(values[handle])];
// ***** Problem line *****
});
}
HTML:
<div id="slider-value"></div>
<div id="slider"></div>
<div id="slider2-value"></div>
<div id="slider2"></div> (etc...)
The problem line is highlighted above ... when using a static value (ex, 'slider2-value') it works fine, but I can't seem to figure out how to target the appropriate id when the update event triggers ... slider_names[i] obviously won't work there. I'm probably missing something obvious? Thanks!