0

I asked a question earlier about how to "Add a class to a button if aria-valuenow = 0" and I got a solution but I assumed that it would change when the slider in the input goes to zero.

It only works what was loaded in aria-valuenow. For example, if aria-valuenow=50 was loaded then the code won't work but if area-valuenow=0 is loaded, then it works.

So how do you add a class to a div based on an input where the value is programmatically changed?

Code:

<div class="plyr__control js-volume" data-plyr="mute">My Button</div>
<input data-plyr="volume" type="range" min="0" max="1" step="0.05" value="1" 
autocomplete="off" role="slider" aria-label="Volume" aria-valuemin="0"
aria-valuemax="100" aria-valuenow="*programmatically changes*" id="plyr-volume-2587" style="--value:0%; 
width: 0px; left: 200px;" aria-valuetext="0.0%" class="plyr__tab-focus">

JS Code:

$(function() {
var val = $('.plyr__volume input').attr('aria-valuenow');
if(val == "0") $('.js-volume').addClass('test');
})();

Working example with input changing values - https://codepen.io/openbayou/pen/JQeEry

Gregory Schultz
  • 864
  • 7
  • 24

1 Answers1

2

I think you do not need extra attribute (aria-valuenow), you can check the value on input event based on which you can add/remove the class:

Demo:

(function() {
  $('#plyr-volume-2587').on('input',function(){
    if(this.value == '0')
      $('.js-volume').addClass('test')
    else
      $('.js-volume').removeClass('test')
  });
  $('#plyr-volume-2587').trigger('input');
})();
.test{
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="plyr__control js-volume" data-plyr="mute">My Button</div>
<input data-plyr="volume" type="range" min="0" max="1" step="0.05" value="1" 
autocomplete="off" role="slider" aria-label="Volume" aria-valuemin="0"
aria-valuemax="100" id="plyr-volume-2587" style="" aria-valuetext="0.0%" class="plyr__tab-focus">
Mamun
  • 66,969
  • 9
  • 47
  • 59