<select id="periodicity"> <option value="3">1-20 </option><option value="25">20-40 </option><option value="50">40-60 </option></select> 

Is it possible to make it so that when choosing for example 1-20 a random value is set from the same interval and similarly 20-40, 40-60

    2 answers 2

     //---------------------------------------------------------------- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random // function getRandomIntInclusive(min, max) { min = Math.ceil(min); max = Math.floor(max); //The maximum is inclusive and the minimum is inclusive return Math.floor(Math.random() * (max - min + 1)) + min; } //---------------------------------------------------------------- var sel = document.querySelector('select'); sel.addEventListener('change', function () { // Get min and max from text var val = sel.selectedOptions[0].text.split('-') // Set value to selected option sel.selectedOptions[0].value = getRandomIntInclusive(+val[0],+val[1]) console.log(sel.value); }) 
     <select id="periodicity"> <option value="3">1-20 </option> <option value="25">20-40 </option> <option value="50">40-60 </option> </select> 

      Put addEventListener "change" on the select and track the value . And further

       // Возвращает случайное число между min (включительно) и max (не включая max) function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min; } 

      MDN: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Math/random