Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Searchbar user focus trigger search › Reply To: Searchbar user focus trigger search
July 2, 2021 at 8:53 am
#33826
Keymaster
Hi,
This custom code should help you triggering the search whenever the input is focused:
add_action('wp_footer', 'wp_footer_asp_custom_script', 9999);
function wp_footer_asp_custom_script() {
?>
<script>
(function(){
document.querySelectorAll('input.orig').forEach(function(el){
el.addEventListener('focus', function(){
el.dispatchEvent(new Event('input'));
});
});
}())
</script>
<?php
}
Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
This code will trigger the search initially, whenever the user clicks on the input field.