简体   繁体   中英

how to increase and decrease the value of range slider input on clicking next and prev button (vanilla JavaScript)

this is the code How to increase/ decrease value by 20?

<div class="slider">
    <input type="range" min="1" max="100" value="0" class="img__slider" id="myRange">
</div>

<div>
    <button id="prev">Prev</button>
    <button id="next">Next</button>
</div>

js code

const prevbtn = document.querySelector('#prev');
const nextbtn = document.querySelector('#next');
const rangeValue = document.querySelector('#myRange');

nextbtn.addEventListener('click', () => {
  let getValue = rangeValue.value;

  getValue += 20
})

prevbtn.addEventListener('click', () => {
  let getValue = rangeValue.value;

  getValue -= 20
})

I tried increasing the value on click but it doesnt work

Use step attribute

    <input type="range" min="1" max="100" value="0" step="20"class=" img__slider" id="myRange">
</div>

<div>
    <button>Prev</button>
    <button>Next</button>
</div>

You should setAttribute method:

const prevbtn = document.querySelector('#prev');
const nextbtn = document.querySelector('#next');
const rangeValue = document.querySelector('#myRange');

nextbtn.addEventListener('click', () => {
  rangeValue.setAttribute('value', rangeValue.value + 20)
})

prevbtn.addEventListener('click', () => {
  rangeValue.setAttribute('value', rangeValue.value - 20)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM