DEV Community

Cover image for How To: Build A Simple Search Bar in JavaScript

How To: Build A Simple Search Bar in JavaScript

Adriana DiPietro on November 15, 2021

Hi Everyone! Today, we will be walking through how to build a functional, yet very simple search bar in JavaScript. This mini project will also ut...
Collapse
 
jdboris profile image
Joe Boris • Edited

EDIT: Edited to sound nicer

Good article. Here are some tips...

You should define the list variable manually. You technically don't need to, because of a little-known feature that automatically defines a global variable for each HTML element with an ID, but it's a bad practice to rely on that.

It looks like you may have made a mistake in the section where you introduce clearList. Your example code still has the placeholder comment for step 4 instead of calling setList.

Your clearList function is overcomplicated. You can just use list.innerHTML = "".

Try to follow more standard conventions and patterns. Instead of setList, I would name it renderList since showing data on the page is referred to as rendering.

Checking the length of the textbox value is unnecessary, because an empty string is already falsy. if( value.trim() ) is sufficient.

I prefer to use append rather than appendChild since it's shorter.

Lastly, you can't go wrong with document.createElement, but I prefer to use innerHTML when possible since it's shorter. You just have to remember that it clears event listeners on children.

Collapse
 
am20dipi profile image
Adriana DiPietro

I appreciate the feedback, but there are ways of providing "criticism" as you claim without condescension. Yes -- there are many ways to achieve a goal with code -- we all know that. This is a beginner's ~guide~... guide meaning do things the way you would like and follow my outline to your desired effect. It's nice to see that we all can think and improve on others; but try to say it nicer. Also "shorter" does not equate to better. Sometimes "longer" improves readability, especially for beginners. Nonetheless, I appreciate you taking the time to read and comment on my post. I like continuing the conversation past the post :)

Collapse
 
zippcodder profile image
Deon Rich

I agree. Good article, great project for people still starting out.

Thread Thread
 
am20dipi profile image
Adriana DiPietro

Thank you Deon!

Collapse
 
travissanon profile image
Travis Sanon • Edited

I'd say that

function clearList() {
  list.innerHTML = ''
}
Enter fullscreen mode Exit fullscreen mode

Is easier to read than

function clearList() {
    while (list.firstChild){
        list.removeChild(list.firstChild)
    }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
alexfstack profile image
Alex-FStack • Edited

Boris made some great points on how to improve the code, did not see anything 'condescending' in there, all positive criticism and helpful quality tips, seems you cant take any... attitude problem?

Collapse
 
eliamazzon profile image
Elia Mazzon

Italian fella here, grande grazie!

Collapse
 
am20dipi profile image
Adriana DiPietro

Thanks Elia!

Collapse
 
tannazdev profile image
TannazDev

Thank you for your good content but there's a question in my mind I just want to make sure that the "e" parameter inside of this arrow function refers to the event that is happening is that correct??

searchInput.addEventListener("input", (e) => {}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
thedevkushagra profile image
KUSHAGRA DUBEY

Yes, "e" represents the event that is happening here.

Collapse
 
safayetjamil647 profile image
Safayet Jamil

Nice article for javascript developer.

Collapse
 
am20dipi profile image
Adriana DiPietro

Thank you Safayet!

Collapse
 
thumbone profile image
Bernd Wechner

I get a 404 on that link 🤔

 
thumbone profile image
Bernd Wechner

Thanks. But I'm curious, why are you posting a link to an unpublished article?

Collapse
 
imiahazel profile image
Imia Hazel

Thanks :)

Collapse
 
am20dipi profile image
Adriana DiPietro

Thanks for the comment Imia!

Collapse
 
bhavyashah profile image
Bhavyashah

Nice explanation Adriana but if you add some css then it looks like a full tutorial. Bts no problem this also looks cool. Thanks for sharing ☺️

Collapse
 
am20dipi profile image
Adriana DiPietro

Thanks for commenting!!! Yes -- I admit I got a little tired at the end haha

Collapse
 
kimo871 profile image
kimo871

Cool !

Collapse
 
am20dipi profile image
Adriana DiPietro

Thanks for commenting :)

Collapse
 
thedevkushagra profile image
KUSHAGRA DUBEY

Thanks for the Knowledge....Really Helpful

Collapse
 
am20dipi profile image
Adriana DiPietro

Thanks for the comment!

Collapse
 
rmanantsoa profile image
Tianamanantsoa RAMBELOSON

thanks

Collapse
 
am20dipi profile image
Adriana DiPietro

thanks for the comment :)

Collapse
 
alotfy2019 profile image
Ahmed Lotfy

Ur war of explaining is great and simple thnx for ur effort

Collapse
 
am20dipi profile image
Adriana DiPietro

Thanks Ahmed! I appreciate it

 
thumbone profile image
Bernd Wechner

Cool Magoo. Fix it and publish it then ;-)

Collapse
 
obaino82 profile image
Obaino82

Thanks for sharing

Collapse
 
am20dipi profile image
Adriana DiPietro

Thanks for the comment :)

Collapse
 
iakovosvo profile image
iakovosvo

spammer

Collapse
 
jacksonemmanuel profile image
Jackson

Thank you for this.
Is there a reason for not using
<input type="search" />?

Collapse
 
am20dipi profile image
Adriana DiPietro

Both get the job done, yes! They are functionally identical. However, your way definitely may improve readability.

Collapse
 
femmeinstem profile image
Meia

So helpful, can't wait to try! Thank you!

Collapse
 
am20dipi profile image
Adriana DiPietro

Thank you Meia!

Collapse
 
hammadjamil92 profile image
Hammad Jamil

Nice

Collapse
 
am20dipi profile image
Adriana DiPietro

Thanks for the comment Hammad!

Collapse
 
gabriel_chukwu_9df4111800 profile image
Gabriel Chukwu • Edited

Nice work 👍

But, there is something that I noticed
The event listener acts per every input without refreshing, thereby giving repeated result.

I hope there could be a way to refresh the results at every input such that the result shown will only be the expected ones.

To achieve this , you just add
list.innerHTML= " "
at the toppest side before everything in the event listener code, like this

searchInput.addEventListener("input", (e) => {
//Here
list.innerHTML
// inside, we will need to achieve a few things:
// 1. declare and assign the value of the event's target to a variable AKA whatever is typed in the search bar
let value = e.target.value

// 2. check: if input exists and if input is larger than 0
if (value && value.trim().length > 0){
    // 3. redefine 'value' to exclude white space and change input to all lowercase
     value = value.trim().toLowerCase()
    // 4. return the results only if the value of the search is included in the person's name
    // we need to write code (a function for filtering through our data to include the search input value)
} else {
    // 5. return nothing
    // input is invalid -- show an error message or show no results

}
Enter fullscreen mode Exit fullscreen mode

}
Thanks.

Collapse
 
maryanne_fusco profile image
Mary Anne Fusco

Thank you, Adriana! I was struggling with a search bar for my site. It was working somewhat. This article helped me to get it to work seamlessly! Kudos to you!

Collapse
 
am20dipi profile image
Adriana DiPietro

Thank you Mary Anne! It is great you kept trying even when you faced obstacles. Congrats!!!

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
am20dipi profile image
Adriana DiPietro

Thank you for the comment Jahid!

Collapse
 
am20dipi profile image
Adriana DiPietro

Thanks you Sagar. I appreciate your comment :)

 
thumbone profile image
Bernd Wechner

You know you can edit your original comment and fix the link there? Just checking.

Collapse
 
spikedhelmet profile image
Zaur Hasanov

Thank you ✌

I got everything from other tutorials, but I was getting repeat results, but thanks to your code I finally figured it out!

Collapse
 
sanyasingla9 profile image
Sanya Singla

can u pls share zip file of this design.