Skip to content
Advertisement

Search Dropdown Javascript – How to hide list?

I’m a beginner in Javascript/HTML/CSS. So I’m sorry if I’m missing something obvious.

My Task: I want to create a search bar on my website (website for images). With this search bar the user should be able to search for “keywords” which I will determine in a list. Every item in my list should lead the user to a new html site.

My Problem: The list that i have created is visible under the search input. But I want to hide the list at first and just show the suggestions or matches from what the user enters in the search bar. But if I hide the list in css, it’s obviously also not gonna show the matches when theres something typed in the input. I included code and screenshots at the bottom. Thanks!

JavaScript

Screenshots: All the suggestions from my list are showing, how can i hide them if nothing is typed in the input?

Advertisement

Answer

You can use the same idea you’ve already used in the <li>, use display: none to hide the element, then on every call of myFunction(), check the content of the <input> and edit the <ul> style accordingly:

JavaScript

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement