Search code examples
jqueryjquery-1.9jquery-migratejquery-trigger

trigger.focus replaced by $("selector").get(0).focus() in jquery1.9?


When you .trigger("focus") in IE, jQuery won't "see" the async focus event which will occur later, so it fires one of its own to ensure that a focus event always occurs as described above. This causes two calls to the event handler. To avoid this double-call--but risk that the event handler is not called at all--use the DOM focus method directly, e.g., $("selector").get(0).focus().

This is what jquery migrate site says. Although I was not clear if every trigger.focus needs to be replaced with new implementation? IF so what would be the new code be for a simple button. trigger focus?

 $('#btnMove').trigger('focus');

Solution

  • Read Order of triggered "focus" events

    Get Native DOM Element and call focus event on it.

    $('#btnMove').get(0).focus();
    

    Or

    $('#btnMove')[0].focus();
    

    Or Use Pure JavaScript

    document.getElementById('btnMove').focus();