I am making a table form where the user will adjust multiple sliders in the table. I want to update the value to be displayed on the screen for each slider. I tried to do this with getElementsByClassName
, but when I adjust one slider, the output value is shown in all of the other sliders. I guess I can assign an id to each slider (there are about 20), but I was wondering if there was a more efficient way to do this.
Here is a sample of the html code for the table inputs:
<form>
<table>
<tr>
<th rowspan="5" id="A"></th>
<td></td>
<td><input type="range" min="0" max="10" value="5" step="1" onChange="sliderChange(this.value)"/><br />Current Value: <span class="sliderStatus">5</span></td>
<td></td>
<td><input type="range" min="0" max="10" value="5" step="1" onChange="sliderChange(this.value)"/><br />Current Value: <span class="sliderStatus">5</span></td>
</tr>
<tr>
<td></td>
<td><input type="range" min="0" max="10" value="5" step="1" onChange="sliderChange(this.value)"/><br />Current Value: <span class="sliderStatus">5</span></td>
<td></td>
<td><input type="range" min="0" max="10" value="5" step="1" onChange="sliderChange(this.value)"/><br />Current Value: <span class="sliderStatus">5</span></td>
</tr>
<tr>
<td></td>
<td><input type="range" min="0" max="10" value="5" step="1" onChange="sliderChange(this.value)"/><br />Current Value: <span class="sliderStatus">5</span></td>
<td></td>
<td><input type="range" min="0" max="10" value="5" step="1" onChange="sliderChange(this.value)"/><br />Current Value: <span class="sliderStatus">5</span></td>
</table>
</form>
and the JS Code:
//called when the user clicks the start button on the home page.
function start(){
var decisionA = prompt("Enter A:","");
var decisionB = prompt("Enter B:","");
//send user prompt inputs to A and B cells in the table<th>.
document.getElementById('A').innerHTML = decisionA;
document.getElementById('B').innerHTML = decisionB;
}
function sliderChange (val){
var sliders = document.getElementsByClassName('sliderStatus');
for (i = 0; i < sliders.length; i++){
sliders[i].innerHTML = val;
}
}
Is there a way to make the slider value text show the adjusted slider value for each individual slider in the table using getElementsByClassName
or do I need to assign 20 different ids to all the sliders? Thanks.