Friday, 26 July 2013

Jquery is(':focus') is not working in IE8

I recently encountered a problem when using the is(':focus') with ie8. The only error I got is

SCRIPT5022: Exception thrown and not caught 
jquery.js, line 85 character 139

Not a lot of help.

After further research I found that there is actually a problem with how jQuery is handling this and I had to rewrite it.

jQuery.expr[':'].focus = function( elem ) {
    return elem === document.activeElement && ( elem.type || elem.href );

Quoted from Mathias Bynens here:
Note that the (elem.type || elem.href) test was added to filter out false positives like body. This way, we make sure to filter out all elements except form controls and hyperlinks.