1

I am trying to get access to the changing values of a series of HTML range sliders.

Here is how they're instantiated:

<li *ngFor="let rgbVal of rgbVals; let i=index">
            {{i}}: {{rgbVal}} <br>
            <div class="color-box" [style.backgroundColor]="rgbVal"></div>
            <div class="slideContainer" >
                <input type="range" min="1" max="100" value="30"  class="slider" id="shade{{i}}" step="1" (change) = "update(i)"  >

            </div>


        </li>

This is the Typescript function I have in my .ts function that should be able to access the value of the sliders:

update(i){
var id = "shade"+i;
var slider = document.getElementById(id);
console.log(slider.getAttribute("value"));}

The value once I move the slider stays constant & unchanged, no matter which slider I move or how much I move it.

Interestingly enough, if I run this code:

 update(i){
var sliders = document.getElementsByClassName("slider");
console.log(sliders);}

And then in the console open up the HTML container and look at the value field of each slider, it is updated as I move it.

I also tried

slider = sliders[i];
console.log(slider.getAttribute("value");

And that stayed constant regardless of change, even though that slider when viewed as a part of the sliders object allowed the value to be updated.

I am baffled by this one and can't find any similar errors online, so any help would be greatly appreciated.

0

1 Answer 1

1

You problem is that slider.getAttribute("value") reads the value of the attribute, not the value of the input, so it will always say 30.

What you need is:

update(i: number) {
    const id = `shade${i}`;
    const slider = document.getElementById(id) as HTMLInputElement;
    console.log(slider.value);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.