Is there a 'has focus' in JavaScript (or jQuery)?

Posted by alex on Stack Overflow See other posts from Stack Overflow or by alex
Published on 2010-04-21T14:23:07Z Indexed on 2010/04/21 14:33 UTC
Read the original article Hit count: 922

Filed under:
|

Is there something I can do like this (perhap via a plugin)

if ( ! $('form#contact input]').hasFocus()) {

    $('form#contact input:first]').focus();
}

Basically, set focus to the first input, but only if the user has not already clicked into anything?

I know this will work too, but is there anything more elegant?

$(function() {
    var focused = FALSE:
    $('form#contact input]').focus(function() {
        focused = TRUE;

    }); 

    setTimeout(function() {
       if ( ! focused) {      
            $('form#contact input:first]').focus();
       };
    }, 500);

});

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery