I am trying to validate an input field when it loses focus and re-focus the field if the validation fails. But I cannot seem to get the field to re-focus. I have written a test case at http://jsbin.com/ahepo/edit . It is failing in Firefox 3.6.2 and IE 8, but working in Chrome 4.1.
+1
A:
The only thing I'd come up with is:
$('#field1').focusout(function() {
var $this = $(this);
$this.val('not valid!');
window.setTimeout(function(){
$this.focus();
},1);
});
jerone
2010-04-02 14:26:13
A bit hackish, but it works. Thanks.
jsumners
2010-04-02 14:38:03
A:
Put the call to .focus() in a timeout handler. That way, it happens after the "focusout" completes.
setTimeout(function() { $this.focus(); }, 1);
Pointy
2010-04-02 14:26:40