I am building a custom dropdown menu using JavaScript and CSS, and I want the clear button to reset the selection. However, even after clicking the clear button, the dropdown remains selected, and the selected class is not removed properly.
Expected Behavior:
- Clicking the clear button (
.clear-btn) should: - Remove the selected class from the dropdown.
- Reset the selected text to "Select an option".
- Hide the clear button.
- Close the dropdown list.
Current Behavior:
- The clear button resets the text, but the dropdown remains selected.
- The selected class does not get removed properly.
- The dropdown stays open instead of closing.
Code Below:
function activateDropdowns() {
document.querySelectorAll(".custom-dropdown").forEach(dropdown => {
const list = dropdown.querySelector(".dropdown-list");
const clearBtn = dropdown.querySelector(".clear-btn");
const selectedText = dropdown.querySelector(".selected-text");
dropdown.addEventListener("click", function(event) {
event.stopPropagation(); // Prevents dropdown from closing immediately
if (event.target.classList.contains("clear-btn")) {
console.log("Clear button clicked!"); // Debugging Step 1
dropdown.classList.remove("selected"); // ❌ This doesn't seem to remove the class properly
selectedText.textContent = "Select an option";
clearBtn.style.display = "none";
dropdown.classList.remove("active");
list.classList.remove("show");
} else if (event.target.closest(".dropdown-header")) {
// Toggle dropdown only if clicking on header (not clear-btn)
console.log("Dropdown header clicked");
document.querySelectorAll(".custom-dropdown").forEach(d => {
if (d !== dropdown) {
d.classList.remove("active");
d.querySelector(".dropdown-list").classList.remove("show");
}
});
dropdown.classList.toggle("active");
list.classList.toggle("show");
}
});
});
// Close dropdown when clicking outside
document.addEventListener("click", function() {
document.querySelectorAll(".custom-dropdown").forEach(dropdown => {
dropdown.classList.remove("active");
dropdown.querySelector(".dropdown-list").classList.remove("show");
});
});
}
activateDropdowns();
.custom-dropdown {
position: relative;
width: 200px;
background: #E3DCC3;
/* Neutral beige */
border-radius: 8px;
cursor: pointer;
}
.dropdown-header {
display: flex;
justify-content: space-between;
padding: 10px;
background: white;
}
.clear-btn {
display: none;
cursor: pointer;
}
.custom-dropdown.selected .clear-btn {
display: inline-block;
}
.custom-dropdown.active .dropdown-list {
display: block;
}
<div class="custom-dropdown selected active" id="locationDropdown">
<div class="dropdown-header" tabindex="0">
<span class="selected-text">Sapporo, Hokkaido</span>
<span class="arrow">▼</span>
<span class="clear-btn" style="display: inline-block;">✖</span>
</div>
<ul class="dropdown-list show">
<li>Tokyo</li>
<li>Osaka</li>
<li>Sapporo, Hokkaido</li>
</ul>
</div>
Steps that I have tried:
- Logged the clear button click event → It fires correctly.
- Manually removed the .selected class in DevTools → It works there.
- Tried .classList.toggle("selected") instead of .remove() → No change.
- Checked if any CSS styles are preventing changes → Nothing found.
Question:
Why is my .selected class not being removed properly when I click the clear button?
- Is my event delegation incorrect?
- Could CSS be overriding the class change?
- Am I missing an additional property that needs to be reset?
selectedclass is removed when I check the Elements tab in DevTools. Why do you think it's not working?