Create an input, button and div in which the checkboxes will be:
<input id="input" type="text"> <button onclick={myFunc()}>click</button> <div id="div"></div>
One of the options:
function myFunc() { // берем значение из input var element = document.getElementById('input').value // разбиваем его по знаку "-" var arr = element.split('-') // если длина массива === 2, то все ок if (arr.length === 2) { // переводим в число var num1 = Number(arr[0]) var num2 = Number(arr[1]) // если это оба числа и 1 число меньше 2 то все ок if (!isNaN(num1) && !isNaN(num2) && num1 <= num2) { var divEl = document.getElementById('div') // очищаем все что внутри div while (divEl.firstChild) { divEl.removeChild(divEl.firstChild); } for (var i = num1; i <= num2; i++) { // создаем чекбокс var checkbox = document.createElement('input') checkbox.type = 'checkbox' checkbox.value = i checkbox.id = 'checkbox' + i // создаем label для чекбокса var label = document.createElement('label') label.htmlFor = 'checkbox' + i label.innerHTML = 'checkbox' + i // добавляем в наш div divEl.appendChild(checkbox) divEl.appendChild(label) } } } }
Unfortunately, I don’t know what the conditions are for you, but if you wish, you can not do checks on isNaN, but simply prohibit input, for example, characters, or make some kind of mask on input. It's just not quite obvious what is required.
input, for example,25-28presses a button. After clicking the button, the script should form an array of numbers from25-28and display as<input type='checkbox' value='25'>, etc. to the end of the array. - Aziret Kadykeev