I am using jQuery to check the value of a range slider, and if that value is 1, add a class to an other element. The code below works well for this:
$(document).ready(function() {
$("#range-slider").mousemove(function() {
if (this.value == "1") {
$('.other-element').addClass('is--active');
} else {
$('.other-element').removeClass('is--active');
}
});
});
However I’d like to do this for multiple values, but the below code does not work. So how can I achieve this?
$(document).ready(function() {
$("#range-slider").mousemove(function() {
if (this.value == "1", "2", "3", "4", "5") {
$('.other-element').addClass('is--active');
} else {
$('.other-element').removeClass('is--active');
}
});
});
3
Answers
You need to check if the list of values includes
this.value
:If you’re looking to add a class to an element depending on the range slider’s value, here’s the code:
You can use the following if you want the action to happen for a certain range (10…20):