Не работает атрибут max и событие oninput у input amount

<input name="amount" type="number" max="120" min="1" step="1" id="amount" value="1">

Вот код js

  const $amount = document.getElementById("amount");
        $amount.oninput = function () {
            if ($amount.value < 1 || $amount === "") {
                $amount.value = 1;
            }
            if ($amount.value > $amount.getAttribute("max")) {
                $amount.value = $amount.getAttribute("max");
            }
        };

Если делаю значение меньше 1, то всё работает, а когда больше 120, то иногда работает, но бывают случаи когда не работает код, даже после какого-то времени код не работает. Вот меньше 1 написать не могу, а больше 120 - могу


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

Автор решения: Igor

document.getElementById("amount").oninput = function() {
  if (+this.value < 1 || this.value === "") {
    this.value = 1;
  }
  if (+this.value > +this.max) {
    this.value = this.max;
  }
};
<input id="amount" type="number" max="120" min="1" step="1" id="amount" value="1">

→ Ссылка