I’m currently working on a two-dropdown multiselect menu, and I’m facing a challenge with the dropdown behavior. Specifically, I want the first dropdown and its options to stay fixed at the top, ensuring that clicking on it doesn’t cause the second dropdown to shift downward.
every time I click on the first dropdown menu, the second dropdown menu gets dragged to the bottom
const selectBtn = document.querySelector(".select-btn");
const item = document.querySelectorAll(".items");
selectBtn.addEventListener("click", () => {
selectBtn.classList.toggle("open");
});
item.forEach(items => {
items.addEventListener("click", () => {
items.classList.toggle("checked");
let checked = document.querySelectorAll(".checked"),
btnText = document.querySelector(".btn-text");
if (checked && checked.length > 0 && checked.length <= 1) {
btnText.innerText = `${checked.length} Selected Item`;
} else if (checked && checked.length > 1) {
btnText.innerText = `${checked.length} Selected Items`;
} else {
btnText.innerText = "Select Year and Semester";
}
});
})
:root {
--yellow: #FFD83A;
--gray: #D0D0D0;
--lightGray: #e1e1e1;
--blue: #004AAD;
--black: #141413;
}
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
width: 22rem;
height: 35rem;
overflow: hidden;
}
/*Navbar*/
.navbar h2 {
font-size: 1.4rem;
}
.navbar ul {
list-style: none;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0px 10px;
margin: 15px 4px 5px;
}
.navbar li img {
width: 30px;
height: 30px;
}
/*Header*/
.header p {
text-align: center;
font-size: .8rem;
margin-bottom: 20px;
}
/*drop-down Checkbox*/
.container {
position: relative;
max-width: 290px;
font-weight: lighter;
margin: 0px 30px;
}
.select-btn {
background-color: var(--gray);
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
cursor: pointer;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
margin-bottom: 15px;
}
.select-btn .btn-text {
font-size: 0.9rem;
font-weight: bold;
}
.select-btn .arrow-down {
display: flex;
justify-content: center;
transition: 0.3s;
}
.select-btn.open .arrow-down {
transform: rotate(-180deg);
}
.arrow-down .arrow-icon {
font-size: 1rem;
}
/*drop-down Items*/
.list-items {
position: relative;
border-radius: 5px;
margin-top: 7px;
background-color: var(--gray);
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
font-size: 1rem;
display: none;
}
.select-btn.open~.list-items {
display: block;
}
.list-items .items {
list-style: none;
display: flex;
align-items: center;
padding: 10px;
height: 30px;
cursor: pointer;
border-radius: 5px;
transition: 0.2s;
}
.list-items .items:hover {
background-color: var(--yellow);
}
.items .checkbox {
display: flex;
align-items: center;
justify-content: center;
border: 1.5px solid;
border-radius: 5px;
width: 16px;
height: 16px;
margin-right: 15px;
transition: all 0.3 ease-in-out;
}
.items.checked .checkbox {
color: var(--yellow)
}
.checkbox .check-icon {
font-size: 0.6rem;
transform: scale(0);
transition: all 0.3 ease-in-out;
}
.items.checked .check-icon {
transform: scale(1);
}
<section class="section">
<div class="container">
<div class="select-btn">
<span class="btn-text">Select Year and Semester</span>
<span class="arrow-down">
<i class="fa-solid fa-circle-chevron-down arrow-icon"></i>
</span>
</div>
<ul class="list-items">
<li class="items">
<span class="checkbox">
<i class="fa-solid fa-circle check-icon"></i>
</span>
<span class="item-text">1st Year 1st Semester</span>
</li>
<li class="items">
<span class="checkbox">
<i class="fa-solid fa-circle check-icon"></i>
</span>
<span class="item-text">1st Year 2nd Semester</span>
</li>
<li class="items">
<span class="checkbox">
<i class="fa-solid fa-circle check-icon"></i>
</span>
<span class="item-text">2nd Year 1st Semester</span>
</li>
<li class="items">
<span class="checkbox">
<i class="fa-solid fa-circle check-icon"></i>
</span>
<span class="item-text">2nd Year 2nd Semester</span>
</li>
<li class="items">
<span class="checkbox">
<i class="fa-solid fa-circle check-icon"></i>
</span>
<span class="item-text">3rd Year 1st Semester</span>
</li>
<li class="items">
<span class="checkbox">
<i class="fa-solid fa-circle check-icon"></i>
</span>
<span class="item-text">3rd Year 2nd Semester</span>
</li>
<li class="items">
<span class="checkbox">
<i class="fa-solid fa-circle check-icon"></i>
</span>
<span class="item-text">4th Year 1st Semester</span>
</li>
<li class="items">
<span class="checkbox">
<i class="fa-solid fa-circle check-icon"></i>
</span>
<span class="item-text">4th Year 2nd Semester</span>
</li>
</ul>
</div>
<div class="container">
<div class="select-btn">
<span class="btn-text">Select Subject</span>
<span class="arrow-down">
<i class="fa-solid fa-circle-chevron-down arrow-icon"></i>
</span>
</div>
</div>
</section>
2
Answers
make the
It should work
Hi I have a custom dropdown with checkbox code that is work for me. I know the drowpdown text is not same as you, but you can use it and change your text.