I have an array of inputs, after looping through them using a for loop, is it possible to store each value into a variable?
Here's what I am trying to do specifically: I am building a gradient background generator. I have a total of 6 inputs (rgb for the first color of the gradient and rgb for the second one). I loop through the input array and then I stored each value into a variable (r, g, b, r2, g2, b2) and then use them in the linear-gradient css.
My method doesn't seem to be very functional, so I was wondering if it was possible to store all the input[i] values "automatically" into variables.
const inputs = document.querySelectorAll('input')
const body = document.body;
for (var i = 0; i < inputs.length; i++) {
inputs[i].addEventListener('change', changeValue);
inputs[i].addEventListener('mousemove', changeValue);
function changeValue() {
let r = inputs[0].value;
let g = inputs[1].value;
let b = inputs[2].value;
let rgb = r + ',' + g + ',' + b;
let r2 = inputs[3].value;
let g2 = inputs[4].value;
let b2 = inputs[5].value;
let rgb2 = r2 + ',' + g2 + ',' + b2;
body.style.backgroundImage = `linear-gradient(45deg, rgb(${rgb}), rgb(${rgb2}))`;
};
}
<input type="range" min="0" max="255">
<input type="range" min="0" max="255">
<input type="range" min="0" max="255">
<input type="range" min="0" max="255">
<input type="range" min="0" max="255">
<input type="range" min="0" max="255">