Set Focus to input field

html, jquery

Solution

It works. What's happening is that when your console is open, the console has the focus.

Type this on the console:

setTimeout(function(){
    document.getElementById("leaflet-control-geosearch-qry").focus();
}, 5000);  //Will deffer execution 5 seconds

Then close the console. After 5 seconds, it will focus. So, it's not a coding problem, don't worry ;)

Cheers

Problem

I'm trying to set the focus to the searchbar of this Leaflet Plugin input field, but it does not work. I've tried using the following: ``` //jquery $("#leaflet-control-geosearch-qry").focus(); //html document.getElementById("leaflet-control-geosearch-qry").focus(); ``` You can try it out by yourself. The focus won't be set to set searchbar by using the above mentioned lines. Mapping one of these to a button and clicking on the button will set the focus to the searchbar. ``` <script> //Set Focus to Search Box function setFocusToSearchBar(){ document.getElementById("leaflet-control-geosearch-qry").focus(); //or $("#leaflet-control-geosearch-qry").focus(); } </script> <div id="Focus_on_Searchbar"> <button id="sidebar_button_focus" onclick="setFocusToSearchBar();"> Focus on Searchbar </button> </div> ``` This works! Using one of these with the Chrome/FF Console won't work and I have no idea why not because it works with a button.? Any ideas? I'm using Bootstrap for my Header-Bar...maybe this is important?

Original source