Запретить нажимать на кнопку, если input равен 1 JS

Как запретить нажимать на кнопку, если input имеет значение value = 1 ?


Ответы (3 шт):

Автор решения: dmitriy_vlz
  1. Повесить обработчик на событие onInput для инпута
  2. В нем получать значение инпута и сравнивать его с 1
  3. Если true - кнопке ставишь свойство disabled, иначе убираешь
→ Ссылка
Автор решения: en2wyy

 <input type="text" id ="inputId" oninput="checkValue()" name="input" value="1">
 <button id="buttonId">Check</button>
 <script>
   function checkValue() {
     let input = document.getElementById('inputId')
     let button = document.getElementById('buttonId')
     if (input.value === '1')
       button.disabled = true
     else 
       button.disabled = false
   }
   window.onload = checkValue()
 </script>

→ Ссылка
Автор решения: Михаил Камахин

Просто пример без oninput

const input = document.querySelector('#inputId');
const button = document.querySelector('#buttonId')

function checkValue() {
  button.disabled = input.value === '1';
}

checkValue();
input.addEventListener('input', checkValue);
<input type="text" id="inputId" name="input" value="1">
<button id="buttonId">Check</button>

→ Ссылка