У меня есть случай использования, когда пользователь нажимает на значок поиска, и он отображает ввод.
Я хочу сделать так, чтобы при открытом входе каждый раз, когда вы щелкали за пределами ввода, он скрывал его.
Конечно, я хочу сохранить и тот тумблер, который у меня сейчас есть.
Попытка найти решение в ванильном JavaScript, а не в jQuery.
///////////// Nav - Search Functionality
// Toggle showing and not showing the input
const searchIcon = document.getElementById('search-icon');
const searchInput = document.getElementById('search-input');
searchIcon.addEventListener('click', function() {
searchInput.classList.toggle('is-active');
searchInput.classList.toggle('transform-is-active');
});
.is-active {
display: block !important;
}
.is-hidden {
display: none !important;
}
.transform-is-active {
width: 200px !important;
}
#search-input {
display: none;
margin-right: 10px;
transition: all 2s ease;
}
#search-icon {
cursor: pointer;
}
<!--Search-->
<input id="search-input" class="input" type="text" placeholder="Search...">
<img id="search-icon" src="https://static.thenounproject.com/png/101791-200.png" alt="Search" tabindex="0" />