This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Searchbar user focus trigger search

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Searchbar user focus trigger search Reply To: Searchbar user focus trigger search

#33826
Ernest MarcinkoErnest Marcinko
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.