I have a form where I am trying to set up a dropdown menu below an input field.
My issue is that I can’t get the dropdown menu to be the same width as the input field above it.
If I set the dropdown menu to use relative positioning and I set it to 75% (the same width as the input field) then it works. But the issue then becomes that the dropdown menu pushes down the elements below it when it expands and that is not what I want. I want it to overlay the elements below it when it expands.
So when I set the dropdown div to use relative positioning the width is ok but then it pushes down the elements which is what I dont want. But then when I set it to use fixed positioning it overlays the elements below it ( which is what I want ) but then I cannot get it to have the same width as the input above it.
How do I make this dropdown div BOTH overlay the elements below it when it extends and ALSO be the same exact width as the input field above it?
Ty for your time and God Bless.
here is a reference to the dropdown div and the input field above it
const input = document.getElementById("bizcategory");
const listContainer = document.getElementById("myList");
const words = ['option5', 'option6', 'option7', 'option8',
'option9', 'option10', 'option11'
];
for (let i = 0; i < words.length; i++) {
const newItem = document.createElement("li");
newItem.textContent = words[i];
newItem.style.listStyle = "none";
listContainer.appendChild(newItem);
}
const listItems = listContainer.querySelectorAll("li");
input.addEventListener("focus", () => {
listContainer.style.display = "block";
})
input.addEventListener("blur", () => {
setTimeout(() => {
listContainer.style.display = "none";
}, 200);
});
listItems.forEach(i => i.addEventListener("click", function() {
input.value = i.textContent;
listContainer.style.display = "none";
}));
.signininput {
height: 40px;
width: 75%;
box-sizing: border-box;
}
.list-container {
display: none;
border: 1px solid #ccc;
background-color: white;
width: 75%;
}
.list-container ul {
list-style: none;
padding: 0;
margin: 0;
width: 75%;
}
.list-container li {
padding: 5px;
}
.list-container li:hover {
background-color: #f0f0f0;
cursor: pointer;
}
<input class="signininput" type="text" id="bizcategory" name="bizcategory" placeholder="" maxlength="255" onblur="this.value=removeSpaces(this.value);"></input>
<div class="list-container" id="myList">
<ul style="list-style: none;">
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
</ul>
</div>
4
Answers
First,
input
elements don’t get a closing tag.Next, don’t use inline event handling attributes, like
onblur
. This is a 25+ year old legacy technique. Instead, separate your HTML and JavaScript by setting up events using the modern.addEventListener()
.Also, there is no need to specify the
type
attribute on aninput
if you just want a textbox as that is the default type.Now, to your question, why not assign both elements the same CSS class and set the size there?
Is there some reason why a
select
won’t do what you need here?Here is some example code i drafted which has the behavior you want. I also recommend using JS events, like .addEventListener() or use mouse attributes like onclick, onmouseover, etc.
Add
position: absolute;
to.list-container
which takes it out of the document flow which makes it "float" overposition: static
elements (it’s the default for any element. ie. normal flow). As for the widths the following has been add to the<input>
:<input>
by default will have a differentfont-size
so they have to be set explicitly. The padding is added because<input>
itself is huge. The width was just eyeballed since form fields have browser default styles that do not coincide with normal CSS.I’ve solved it by changing just the css part and added comments to my css changes explaining what each line does.