Set a small time out for focus, and it will work:
setTimeout(function() {
input.focus();
}, 100);
Question
I'm doing inline checking on form input boxes. It's my validating function:
function checkResult(input){
var result = Number(input.value);
if(result < 0 || result > 100){
alert('Invalid!');
input.focus();
}
}
I want to focus on the input (which has lost focus), if it's invalid. That function doesn't work. It says it's Invalid
, but doesn't focus on it.
I set input box onchange
event to checkResut
:
<input type="text" name="tel" id="tel" onchange="javascript:checkResult(this);">
Solution
Set a small time out for focus, and it will work:
setTimeout(function() {
input.focus();
}, 100);