5

I found that many websites provide the shortkey '/' as a way to focus the search box like YouTube, Zhihu, etc. However, I found that it doesn't work on Stack Overflow. This feature is written in front-end JavaScript. Can we add this feature in our website?

<body>
    <input type="text">
    <script>
        var search = document.querySelector('input');
        document.addEventListener('keyup', function(e) {
            // console.log(e.keyCode);
            if (e.keyCode === 191) {
                search.focus();
            }
        })
    </script>
</body>

When I press '/' in my keyboard, I can jump to search box and input.

5
  • 8
    FYI the s key activates the search bar. You can see all keyboard shortcuts by pressing ?
    – Phil
    Commented Aug 11, 2023 at 4:42
  • 5
    Note, to enable the keyboard shortcuts @Phil mentions you can go to this page: stackoverflow.com/users/preferences/current Commented Aug 11, 2023 at 5:36
  • 1
    Thanks you guys. According to your suggestion, I have set up the shortcut for search box.@Phil @Abdul Aziz Barkat Commented Aug 11, 2023 at 8:20
  • @AbdulAzizBarkat Oh yeah, I forgot they have to be enabled
    – Phil
    Commented Aug 11, 2023 at 8:57
  • If enabled in Preferences, they can be discovered by ? (requires Shift on some keyboards). The zoom level must sufficiently low to see all of them. Commented Aug 11, 2023 at 18:21

0

You must log in to answer this question.

Browse other questions tagged .