0

I am trying to set the search on my website as the default focus point on page load. The search I am using is "search & filter"

I have tried using autofocus and onfocus but cannot get the cursor to show in the search box. My search is in a widget on my woocommerce shop page.

  • 1
    Possible duplicate of [jquery focus on load](https://stackoverflow.com/questions/7814361/jquery-focus-on-load) – rob.m Jul 19 '19 at 16:28

1 Answers1

2

If you are using jQuery, you can use the jQuery focus function.

If the focus is not been set on page load, it's likely something else is stealing focus.

function setSearchFocus(){
  console.log('setting search focus');
  $( "#search" ).focus();
}

$( document ).ready(function() {
  setSearchFocus();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="#">
  Search: <input type="text" name="search" id="search"><br/>
</form>
<br/>
<br/>
<button onClick="setSearchFocus()">Click to set focus</button>
user2985710
  • 153
  • 4