Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions myproject/myapp/static/js/countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class CountrySearch {

// Initialize search input and dropdown
init() {
window.onload = () => {
this.handleSearchTerm(); // Handle search term
document.addEventListener("DOMContentLoaded", () => {
this.handleSearchTerm(); // Handle search term //Preferred DOMContentLoaded for better performance and compatibility
this.searchInput.addEventListener("input", this.handleSearchInputChange.bind(this)); // Listen for input changes
this.searchInput.addEventListener("keydown", this.handleSearchInputKeyDown.bind(this)); // Listen for keydown events
};
Expand All @@ -21,7 +21,10 @@ class CountrySearch {
handleSearchInputKeyDown(event) {
const items = Array.from(this.searchDropdown.children); // Convert dropdown children to array
if (items.length === 0) return; // Return if no suggestions

if (event.key === 'Escape') {
this.clearSearchDropdown(); //Use Escape key to close suggestions //new
this.searchInput.blur();
}
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
this.handleArrowKeys(event, items); // Handle arrow key navigation
} else if (event.key === 'Enter') {
Expand Down Expand Up @@ -196,4 +199,4 @@ document.addEventListener("DOMContentLoaded", function () {
if (countrySearch) {
return new RegionSelect(countrySearch); // Initialize RegionSelect with CountrySearch instance
}
});
});