Search code examples
jqueryfocuscss-selectors

How to select an element that has focus on it with jQuery


How can you select an element that has current focus?

There is no :focus filter in jQuery, that is why we can use something like this:

$('input:focus').someFunction();

Solution

  • Really the best way to do it is to setup a handler for the onFocus event, and then set a variable to the ID of the element that has focus.

    something like this:

    var id;
    
    $(":input").focus(function () {
         id = this.id;
    });