Does anyone have experience with Webflow Search and custom triggers in java script for autofocus?
I have a hidden search field inside an overlay which will be opened by a search icon toggle in the navbar. How can I make the search text field autofocus after clicking the toggle?
You probably could do something like capture the click event on the trigger element that the user is first clicking in the navbar, then use the click event to trigger setting focus to another element by ID using the Focus method.
It would look something like this:
// put code in Footer Code section
<script>
$( "#trigger-icon-ID" ).click(function() {
document.getElementById("myTextField").focus();
});
</script>
The “myTextField” is the ID of the text field to focus on, the text field must not be set to display none at time you call this.
I hope this gives you a little something to check and play around with.
Hi @cedi44, yes, if the element is set to display none, the element cannot get focus. There may be errors in the browser console that show what the issue is on the published site if you can help to publish the site, custom code is only rendered on the published page.
Being able to see the behavior on the site helps if you can share the published test url.
Oh yes, its working when I have the wrapper not on display none. But what should I do when my search overlay is all over the regular content? hahah
nothing is clickable anymore beause its not displayed to none anymore and all the site content is below it in z-index.
You will see the page, and as you can see the page below the navbar is not clickable. Thats because there is a transparent white overlay on top of it which you will see as soon you open the search.
I now just removed the full-height overlay of below the search bar and moved the closed search flyout behind the main navbar. Works now, without the white overlay, but this one wasn’t that important. Thank you for your help!