just add onsubmit="return customSearchHandler()" to your form tag and make sure your function returns false to prevent the default submission. way simpler than event listeners. don’t forget to grab the search value with document.getElementById('s').value inside your function or it won’t work properly.
The onclick approach works but you’ll hit problems with form validation and accessibility. I’ve dealt with this on three sites last year - cleanest fix is using jQuery if your theme already loads it.
This keeps your original form markup intact so plugins don’t break, handles search terms properly, and follows WordPress standards. Pass the search term as a parameter instead of trying to grab it from inside the function.
I hit the same issue building a custom search for a client. Your approach bypasses WordPress’s search handling completely, which breaks plugins and themes that hook into the search process.
Ditch the action="javascript:customSearchHandler()" and use an event listener instead. Keep the original form structure but intercept the submission:
This keeps the WordPress form structure while running your custom function. Also, enqueue your JavaScript with wp_enqueue_script() in functions.php rather than dumping it in header.php. Better compatibility and follows WordPress standards.