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);">
Was it helpful?

Solution

Set a small time out for focus, and it will work:

setTimeout(function() {
    input.focus();
}, 100);

DEMO: http://jsfiddle.net/q62m3/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top