Updated: 19 May 2022
This code snippet is to create an auto focus function in the search form when the search button is clicked. In the code sample below, the search button has the id #search_icon
.
I use this code on the Digitalizer My ID website. There is a slight modification so that I can put it in the javascript tab of the Code Block Oxygen Builder component or adding to WP enqueue script.
jQuery(function($){ jQuery('#search_icon').on('click', function(){ setTimeout(function() {jQuery('#s').focus();}, 1000); }); });
If you want to autofocus on page load, you can use this snippet:
setInterval(function(){ if(jQuery('.search_form input').length>0){ jQuery('.search_form input').focus().val(); return false; } }, 500);
Please note you need to change ID, class or tag associated your case.