JavaScript Mapping an HTML5 range slider min and max to array values

I am putting together an HTML5 form for music teachers so they can classify their music library a bit easier. One of the requested elements is a double handled range slider that would be able to define the range of a particular piece. This is all fine if we were talking 0 to 100, but we're talking G-Flat 3 to B5, a series of arbitrary values that wouldn't normally be found on a range slider.

What I'm thinking of doing is creating an array of all the different note ranges from G-Flat 3 to B5 (lowest to highest), and then somehow mapping those to a double handled range slider. Problem is, I don't know how to do that, nor do I know if it is even possible.

In essence, I would like to create an array:

Array = ['Gb3', 'G3', 'G#3', 'Ab3', 'A3',... 'A#5', 'Bb5', 'B5'];

Somehow match those up to numeric values:

0 = 'Gb3';
1 = 'G3';
2 = 'G#3';

And then map those to a double handle range slider.

Any one know if there is a library, or plugin or magic toadstool that can point me in the right direction?

UPDATE: I want to thank EVERYONE who posted to this. There are so many fantastic answers, I'm having a very difficult time choosing "the" answer.

Answer:1

I found this polyfill for a now-abandoned proposal to add a multiple attribute to the <input type="range"> spec. It's simple and works well enough, but has an unfortunate bug: Event handlers aren't fired for when the second handle is moved. It's pretty easy to work around, though:

const notes = ['Gb3', 'G3', 'G#3', 'Ab3', 'A3', /* ... */ 'A#5', 'Bb5', 'B5'];

function handleChange() {
  const { valueLow, valueHigh } = this;
  const lowNote = notes[parseInt(valueLow, 10)];
  const highNote = notes[parseInt(valueHigh, 10)];
  console.log('%s to %s', lowNote, highNote);
}

document.addEventListener('DOMContentLoaded', () => {
  const input = document.querySelector('input[type=range]');
  input.addEventListener('input', handleChange);
  input.nextElementSibling.addEventListener('input', handleChange.bind(input));
});
<script src="https://cdn.rawgit.com/LeaVerou/multirange/gh-pages/multirange.js"></script>
<link target='_blank' href="https://cdn.rawgit.com/LeaVerou/multirange/gh-pages/multirange.css" rel="stylesheet"/>

Change me: <input type="range" min="0" value="0,7" max="7" step="1" multiple>
Answer:2

Using jquery UI slider you can do something like this fiddle:

    $(function () {
    var note =  ['Gb3', 'G3', 'G#3', 'Ab3', 'A3', 'A#5', 'Bb5', 'B5'];
        $("#slider-range-min").slider({
            range: true,
            values: [0,7],
            min: 0,
            max: 7,          
            step: 1,
            slide: function (event, ui) {           
                $("#amount").val(note[ui.values[0]]+ "-"+note[ui.values[1]]);
            }
        });
        $("#amount").val(note[0]+ "-"+note[7]);

});
Answer:3

I have an JS collection (array of objects with few properties). let xyz = [{ categoryId: 1, categoryName: 'Test', programId: 112, programName: 'Program Test', value: 5050, ...

I have an JS collection (array of objects with few properties). let xyz = [{ categoryId: 1, categoryName: 'Test', programId: 112, programName: 'Program Test', value: 5050, ...

Functions that were polyfilled with core-js (for example, babel-polyfill) appear as native. Promise.race.toString() produces: function race() { [native code] } So does Object.values.toString(). ...

Functions that were polyfilled with core-js (for example, babel-polyfill) appear as native. Promise.race.toString() produces: function race() { [native code] } So does Object.values.toString(). ...

  1. core-js babel
  2. core-js babel-polyfill
  3. core-js babel 7
  4. core-js babel webpack
  5. core js babel config
  6. core-js babel-loader
  7. core-js vs babel-polyfill
  8. core-js/stable babel
  9. core-js vs babel
  10. core js 3 babel
  11. babel corejs option
  12. babel core-js version
  13. babel core-js missing
  14. babel core js 2
  15. babel core-js example
  16. babel core-js promise
  17. babel core-js/shim
  18. js babel-core/register

Example HTML page: <div> <ul> <li> 1 </li> <li> 2 </li> </ul> </div> Then in a ajax request I get <div> <ul> <li> ...

Example HTML page: <div> <ul> <li> 1 </li> <li> 2 </li> </ul> </div> Then in a ajax request I get <div> <ul> <li> ...

I am trying to format a number upon input, where you can only insert valid numbers. I have everything working except for the decimal. The input box allows me it insert as many decimals as I would like,...

I am trying to format a number upon input, where you can only insert valid numbers. I have everything working except for the decimal. The input box allows me it insert as many decimals as I would like,...

  1. input allow only numbers
  2. input allow only alphanumeric
  3. input allow only numbers and dot
  4. input allow only numbers angular
  5. input allow only letters
  6. input allow only 2 decimal places
  7. input allow only numbers jquery
  8. input allow only numbers and decimal
  9. input allow only numbers react
  10. input allow only images
  11. input allow only integers
  12. input allow only numbers angular 2
  13. input allow only letters and numbers
  14. input allow only digits
  15. input allow only whole numbers
  16. input allow only numbers and one dot
  17. input allow only positive numbers
  18. input allow only one number
  19. input allow only numbers regex
  20. input allow only two numbers