Jquery.focus not working for input: first

We have a problem when, when opening a modal window, we try to set focus on the first input element in a modal font that does not hide the type. here is what we are trying:

$("#test-overlay input[type!=hidden]:first").focus();

However, this call works:

$("#test-overlay #loginInput").focus();

The input field has the loginInput identifier.

Any thoughts?

+5
source share
1 answer

The problem is with the priority order in which jQuery interprets the selector. Try the following:

$('#test-overlay input').not('[type=hidden]').first().focus();

This has the added benefit of not using :firstan attribute that is not equal to selectors, since they are specific to jQuery, and queries that use them cannot take advantage of the performance boost provided by the DOM querySelectorAll () built-in query method.

+3
source

All Articles