I'm a total javascript newbie. I'm trying to create a search bar that appears when the user clicks on the search icon I've added to the topbar. I used this tutorial
I adapted the HTML markup to my needs, however, when I add the code below (same as in the tutorial), something seems to be going wrong.
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
The search bar appears when the user taps the icon, but if I try to select the search field to write something, it disappears. I checked, and I noticed the same behavior with the tutorial above (if you click one of the links inside the dropdown menu, the dropdown menu disappears).
I know it has something to do with the event target. Does anyone have any idea what should I change and where? The HTML markup is the same, with the exception that, instead of a dropdown menu, I added a search form.
Thank you!