0

I spent about an hour and a half digging through different JavaScripts, and I can't seem to get anything to focus the drop-down search on my website.

I've used this script to trigger the event:

<script type='text/javascript'>
function dropDownSearch(){
   document.getElementById('header-dropdown-search').focus();
}
</script>

On the object: onclick="dropDownSearch()"

On the magnifier icon, I attach an onclick event to execute the script above. However, it never moves the cursor into the search field so the user doesn't have to click. I've also tried doing this by adding an event listener, but that hasn't worked either. With both scripts, I've tested with a pop-up alert. The pop-up works, but the focus doesn't. What am I doing wrong?

This is the URL to the site this is happening on: https://mypomerania.com/getting-started/

  • focus just means that the thing has focus and you have given it all keyboard things and focus events will go to that element. you can sort of control the mouse but don't https://stackoverflow.com/a/63220447/4308987 it's annoying the user when a webpage takes over –  Aug 24 '21 at 03:05

1 Answers1

0

You can try with setTimeout()

function dropDownSearch() {
    window.setTimeout(function () {
        document.getElementById('header-dropdown-search').focus();
    }, 0);
}

source

Lakshaya U.
  • 1,089
  • 1
  • 5
  • 21