Trigger Docsearch with custom button

Is it possible to trigger docsearch with a custom button? I have some text like “Search” in a span tag in my navbar and when the user clicks on that text, I want it to trigger the docsearch functionality.

Are you wanting to execute a search, or just toggle the search modal? Also, are you using a front-end framework? I can supply some code for those as well if needed but I didn’t see you mention one.

If you just wanted to toggle the search popup, you can do something like so:

// Replace `toggleSearch` with your own button identifier

// Vanilla JS
document.getElementById('toggleSearch').addEventListener('click', function() {
  document.getElementsByClassName('DocSearch-Button')[0].click()
})

// jQuery
$('#toggleSearch').on('click', function() {
  $(".DocSearch-Button").click()
})
1 Like