Reputation: 101
I have an <input type="number"
which can store maximum two digits. This is achieved by using event.preventDefault();
.
function allowMaxNumbers(element, event, maxLength){
if(element.value.length==maxLength&& event.keyCode!=8 && event.keyCode!=46 && event.keyCode!=9){
if (event.preventDefault){
event.preventDefault();
}
return false;
}
}
If user selects the input and types in another number, the new number should be inputted replacing the old input. Is there a way in javascript/jQuery to find if some text is selected in the input?
I followed the question How to know if the text in a textbox is selected? and tried input.selectionStart
and input.selectionEnd
but that is not working for input type="number"
Upvotes: 1
Views: 2600
Reputation: 136
selectionStart, selectionEnd are not allowed for type number. you have to change type to text or other valid type.
Check it out : http://www.whatwg.org/specs/web-apps/current-work/multipage/the-input-element.html#do-not-apply
Also check this : selectionStart/selectionEnd on input type="number" no longer allowed in Chrome
Upvotes: 0
Reputation: 12485
You can use tel instead of number like:
<input type="tel" name="anything" />
So your edit box still accepts only number and input.selectionStart and input.selectionEnd are valid.
Please see this link: Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('number') does not support selection
Upvotes: 2