This is a project that, I wanted to create a time ago and is like the title states, a Search Bar using Javascript, so let´s go there.
First we create a new directory of folder with the name Search-Bar in our User folder or C: in the computer , and you can use any text editor that you like, and inside that folder we created we create a file name index.html so we can display the search bar in a browser and we start by typing the head inside the index.html file
Now take a look of what we did above, we started our html like the normal DOCTYPE and inside the head we have linked bootstrap to our job, this will work so we can organize our different options on the search bar, now we continue to type the body of our html file.
CAT
ELEPHANT
BIRD
FISH
LION
TIGER
DOLPHIN
SHARK
MONKEY
FLY
LEOPARD
GORILLA
TUCAN
SNAKE
ANT
CATERPILLAR
Now that we have typed our body, let´s dive into our Javascript file, so we can add functionality and it can display our options that match what we have type in the search bar, that is basically our input tag that is located at the beginning of our body, that we just typed, in the middle you will see the rows and columns of Bootstrap, if you have any questions about what rows and columns are I give the link right here so you can loop it up Bootstrap Grid and if you check at the end of the body we have already link our js file with the name of index.js.
Now we go back to our Search-Bar folder and we create our file name index.js, that is the one that is going to hold our Javascript.
We start our Js code inside the file index.js with the function Search that is going to be called each time that we type in our input tag in the html file, and if we take a look in our input tag has this onkeyup=\"search()\" that is basically the event that we use to call the function.
Now inside our function search we create a variable with the name of field, that is going to get whatever value is in our input.
function search() {
var field = document.querySelector("input").value;
}
And we create another variable with the name of square just below the previous variable that we created, and is going get all the element in our html elements with the class square.
var square = document.getElementsByClassName("square");
Next we create two more variables just below the ones we already created, don´t worry that this are going to be the last variables that we are going to create, the first variable with the name of firstC equals to of getting whatever value that the field variable has to lower case and the other variable with the name of text get all the elements by the tag of tag h3 in the html file.
var firstC = field.toLowerCase();
var text = document.getElementsByTagName("h3");
Now so far our code should look like this.
function search() {
var field = document.querySelector("input").value;
var square = document.getElementsByClassName("square");
var firstC = field.toLowerCase();
var text = document.getElementsByTagName("h3");
}
After we have typed all of this are going to create a for loop, so it can loop trough our array set by the variable text that basically is all the elements that have h3 in out html.
for (var i = 0; i < text.length; i++) {
}
Now inside our for loop we create the variable with the name toL that is going to hold each element while it loops trough the array and it will get the text with innerHTML and it will turn it to lower case.
var toL = text[i].innerHTML.toLowerCase();
Then after we created it our variable we create our if and else statement , the if is going to check the element that we have right now in our loop and we are going is going to check if the characters on the input represented with the variable firstC on the indexOf method that is applied to the element on the loop like this.
if (toL.indexOf(firstC) > -1) {
} else {
}
Now if on of the letters that we have type in the input html match in of the letters or sequence in the elements with the tag h3 represented with the variable text, now if there is not match the value that is going to be returned is -1, for that reason we added > -1
And inside the if statement we add the we add style display to nothing the h3 element and to the parent that is a div to nothing as well, so the elements can be shown if a match is find.
if (toL.indexOf(firstC) > -1) {
text[i].style.display = "\";
text[i].parentNode.style.display = "\";
} else {
}
After we need to fill the else, that is what happens when a match is not found and it will be set the display style to none.
text[i].parentNode.style.display = "none";
And our Javascript should look like this.
function search() {
var field = document.querySelector("input").value;
var square = document.getElementsByClassName("square");
var firstC = field.toLowerCase();
var text = document.getElementsByTagName("h3");
for (var i = 0; i < text.length; i++) {
var toL = text[i].innerHTML.toLowerCase();
if (toL.indexOf(firstC) > -1) {
text[i].style.display = "\";
text[i].parentNode.style.display = "\";
} else {
text[i].parentNode.style.display = "none";
}
}
}
And last but not list we add a little bit of style to our project, we go back to the Search-Bar folder and we create a file with the name of index.css and we type the following code.
body {
background-color: #E26A6A !important;
}
h1 {
color: #D91E18 !important;
text-align: center;
margin-top: 3% !important;
margin-bottom: 3% !important;
}
h3 {
padding: 0%;
color: #F4B350;
}
input[type=text] {
display: block;
margin: 0 auto;
}
.square {
display: inline-block;
margin: 1% 3% 1% 3%;
padding: 2%;
border: 1px solid white;
background-color: #D91E18;
border-radius: 10px;
text-align:center;
}
.hidden {
visibility: hidden !important;
position: absolute;
}
.inline {
display: inline-block;
}
div.all {
margin-top: 2%;
margin-bottom: 2%;
margin-left: 41.5%;
}
And because is already link to or html when we created it at the beginning of this post, try to run on your browser an test it.