jquery focus command doesn’t work from chrome command line

If I have a text page as described below. The call to $("#target").focus(); in the $() section in the header works just fine. I can also bind that command to events in a more realistic page.

However, if I open the chrome console and type $("#target").focus(); it does not change focus. If I run $("#target").val("something"); it changes the value on the screen, but it does not work with focus.

Obviously this isn’t a critical problem, but I am really curious why this happens. Anyone have an idea?

    <script src="https://stackoverflow.com/questions/14783585/jquery-1.9.1.js"></script>

   <input id="target" type="text">

You will realize that when clicking on the Chrome console, it will steal focus from any input or textarea control on the current page, and vice versa. That’s because the Chrome console actually is implemented using the same HTML controls as any other HTML page, just with special properties which, for instance, prevent Chrome from inspecting the Chrome console recursively.

When you type a command in the Chrome console, i.e. in the input control that is part of the Chrome console, it will keep the focus. The Chrome engineers might have chosen to implement it differently, but in most cases the user will want to continue typing in the Chrome console after firing a command, so no command will release focus from the console.

kind of hackish but works…

try to wrap your highlight code with setTimeout :D and before the timer expire click on the web page.

ele2 = $('#input')

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts