Skip to content

Commit

Permalink
docs: add listener only if element exists (#16045)
Browse files Browse the repository at this point in the history
* docs: add listener only if element exists

* docs: check before accessing
  • Loading branch information
amareshsm committed Jun 24, 2022
1 parent 8b639cc commit f5d63b9
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions docs/src/assets/js/search.js
Expand Up @@ -92,33 +92,36 @@ function displaySearchResults(results) {
//-----------------------------------------------------------------------------

// listen for input changes
searchInput.addEventListener('keyup', function (e) {
const query = searchInput.value;

if(query.length) searchClearBtn.removeAttribute('hidden');
else searchClearBtn.setAttribute('hidden', '');

if (query.length > 2) {
fetchSearchResults(query)
.then(displaySearchResults)
.catch(clearSearchResults);
if(searchInput)
searchInput.addEventListener('keyup', function (e) {
const query = searchInput.value;

if(query.length) searchClearBtn.removeAttribute('hidden');
else searchClearBtn.setAttribute('hidden', '');

if (query.length > 2) {
fetchSearchResults(query)
.then(displaySearchResults)
.catch(clearSearchResults);

document.addEventListener('click', function(e) {
if(e.target !== resultsElement) clearSearchResults();
});
} else {
clearSearchResults();
}
});

document.addEventListener('click', function(e) {
if(e.target !== resultsElement) clearSearchResults();
});
} else {
clearSearchResults();
}
});
if(resultsElement)
resultsElement.addEventListener('keydown', function(e) {
if(e.key === "Escape") {
clearSearchResults();
}
}, true);

resultsElement.addEventListener('keydown', function(e) {
if(e.key === "Escape") {
if(searchClearBtn)
searchClearBtn.addEventListener('click', function(e) {
searchInput.value = '';
searchInput.focus();
clearSearchResults();
}
}, true);

searchClearBtn.addEventListener('click', function(e) {
searchInput.value = '';
searchInput.focus();
clearSearchResults();
});
});

0 comments on commit f5d63b9

Please sign in to comment.