Sheancz's avatar

Quick search

Hello! I would love to have option to trigger quick search on Laracasts website (like cmd + K or whatever).

Thank you! You rock.

0 likes
2 replies
LaryAI's avatar
Level 58

To implement a quick search feature on a website like Laracasts, you can use JavaScript to listen for a specific keyboard shortcut (e.g., Cmd + K or Ctrl + K) and then focus on a search input field. Here's a basic example of how you might implement this:

  1. HTML: Add a search input field to your page.
<input type="text" id="quick-search" placeholder="Search..." style="display:none;">
  1. JavaScript: Listen for the keyboard shortcut and focus on the search input.
document.addEventListener('keydown', function(event) {
    // Check if the user pressed Cmd + K (Mac) or Ctrl + K (Windows/Linux)
    if ((event.metaKey || event.ctrlKey) && event.key === 'k') {
        event.preventDefault(); // Prevent the default action (e.g., opening the browser's search)
        
        const searchInput = document.getElementById('quick-search');
        searchInput.style.display = 'block'; // Make the search input visible
        searchInput.focus(); // Focus on the search input
    }
});
  1. CSS: Optionally, you can style the search input to make it more user-friendly.
#quick-search {
    width: 100%;
    padding: 10px;
    font-size: 16px;
    margin-top: 10px;
}

This code snippet will allow users to press Cmd + K (on Mac) or Ctrl + K (on Windows/Linux) to trigger the search input field, making it visible and focusing on it for quick searching. You can further enhance this by adding features like search suggestions or integrating it with your site's search functionality.

Sinnbeck's avatar

The shortcut on laracasts is just the S key

Please or to participate in this conversation.