I'm trying to connect my input range value change with an output. I mean when I move the range input the current value should be shown in the site. I tried it via using form with "oninput" and <output>. It works but I would like to know how to use it with JS. When I use forEach, the one input value which I change applies on other inputs. How to change only the corresponding output?
Below the image
)
My code:
window.addEventListener("DOMContentLoaded", () => {
const filters = document.querySelectorAll(".filters input"),
output = document.querySelectorAll("output");
function handleUpdate() {
const suffix = this.dataset.sizing || "";
document.documentElement.style.setProperty(
`--${this.name}`,
this.value + suffix
);
output.forEach((out) => {
out.value = this.value;
});
}
filters.forEach((input) => input.addEventListener("change", handleUpdate));
filters.forEach((input) => input.addEventListener("mousemove", handleUpdate));
});
label {
display: block;
}
<div class="filters">
<label>
Blur:
<input
name="blur"
data-sizing="px"
type="range"
min="0"
max="10"
value="0"
id="blur"
/>
<output name="result" for="blur">0</output>
</label>
<label>
Invert:
<input
name="invert"
data-sizing="%"
type="range"
min="0"
max="100"
value="0"
id="invert"
/>
<output name="result">0</output>
</label>
<label>
Sepia:
<input
name="sepia"
data-sizing="%"
type="range"
min="0"
max="100"
value="0"
id="sepia"
/>
<output name="result">0</output>
</label>
<label>
Saturate:
<input
name="saturate"
data-sizing="%"
type="range"
min="0"
max="200"
value="100"
id="saturate"
/>
<output name="result">100</output>
</label>
<label>
Hue rotate:
<input
name="hue"
data-sizing="deg"
type="range"
min="0"
max="360"
value="0"
id="hue"
/>
<output name="result">0</output>
</label>
</div>