I have a number input: <input type = 'number>
, and I needed to know how to limit the amount of integers that you could insert into the input
So I came up with what's in the snippet, but are there any other solutions that may be faster to implement, or just better in general.
This question has a similar answer, but I am trying to scour the internet to see if there is a better answer to it.
EDIT: I have found the answer that works to it, and I am looking for something of a one liner that would work. Something like the max = '4'
or the input's version of it maxlength = '4'
const Input = document.getElementById('input')
function limitDigits(element, amount) {
element.addEventListener('input', () => {
if (element.value.length > amount) {
element.value = Math.floor(element.value/10)
}
})
}
limitDigits(Input, 4)
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
#input {
width:30px;
}
<input id = 'input' type = 'number'>