I’m trying to get my search bar to work when pressing enter. It works when pressing the button but not when you press enter….
/**
* search toggle
*/
const searchTogglers = document.querySelectorAll("[data-search-toggler]");
const searchBox = document.querySelector("[data-search-box]");
for (let i = 0; i < searchTogglers.length; i++) {
searchTogglers[i].addEventListener("click", function () {
searchBox.classList.toggle("active");
});
}
<div class="search-container" data-search-box>
<div class="input-wrapper">
<input type="search" id="search" autocomplete="off" aria-label="search" placeholder="Search here..." class="search-field">
<button class="search-submit" type="submit" id="button" onclick="openPage(event)" value="Chercher" aria-label="submit search" data-search-toggler>
<ion-icon name="search-outline"></ion-icon>
</button>
<button class="search-close" aria-label="close search" data-search-toggler></button>
</div>
</div>
2
Answers
Just wrap in a form element
Use Keypress event on searchbox.