Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery focus() is not focusing in IE but it is in Chrome

Tags:

jquery

Here is my code:

jQuery('#reporter').blur(function() {
            if(data.indexOf('['+jQuery('#reporter').val()+']') >= 0)
            {
                alert("Please do not select pseudo user as Reporter");
                jQuery('#reporter').focus();                    
            }               
        });

In IE, the cursor is not blinking in the "reporter" element. In Chrome, it is.

Thanks a lot!

like image 904
chintu Avatar asked Nov 03 '11 14:11

chintu


1 Answers

You'll need to set the blur later by using a timeout. The other control might execute focus first.

Suggestion

window.setTimeout(function(){
   $('#reporter').focus();
}, 50);

This gives IE the time to focus the other control, steal the focus and then add it to #reporter.

Prevent action

$('#reporter').blur(function(e) {
    if(data.indexOf('[' + jQuery('#reporter').val() + ']') >= 0) {
        alert("Please do not select pseudo user as Reporter");
        $('#reporter').focus();
        e.preventDefault();
    }
});
like image 151
Kees C. Bakker Avatar answered Oct 13 '22 00:10

Kees C. Bakker