<div class="range" data-max="100">0-100</li>
<div class="range" data-max="200">0-200</li>
<div class="range" data-max="300">0-300</li>
I wish to access data-max in my javascript. For example, if I click 0-200, I get 200 only. Here is my javascript.
const ranges = document.querySelectorAll(".range");
for(var i = 0; i < ranges.length; i++){
ranges[i].addEventListener('click', function(){
console.log(this.dataset.max);
});
}
The output when I click 0-200 is
200 100
I'm expecting 200.
Here are my questions:
1. how to get expected value---200 as in the example.
2. What is the scope of this in this.dataset.max in my code? I thought its scope is the function.
3. When I change this to ranges[i], it seems ranges[i] is undefined. why?
guessNum.js:4 Uncaught TypeError: Cannot read property 'dataset' of undefined
I'm expecting answers in pure javascript. No jquery pls.