let's build a Book Search page Refer to the below image.
https://assets.ccbp.in/frontend/content/dynamic-webapps/book_search_output.gif
achieve functionality.
PLEASE WRITE HTML AND JAVASCRIPT CODE FOR THIS...
But, here you can see how to do Search Bar:
index.html
<!DOCTYPE html>
<html>
<head>
<title>
Creating Search Bar using HTML
CSS and Javascript
</title>
<!-- linking the stylesheet(CSS) -->
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body>
<!-- input tag -->
<input id="searchbar" onkeyup="search_animal()" type="text"
name="search" placeholder="Search animals..">
<!-- ordered list -->
<ol id='list'>
<li class="animals">Cat</li>
<li class="animals">Dog</li>
<li class="animals">Elephant</li>
<li class="animals">Fish</li>
<li class="animals">Gorilla</li>
<li class="animals">Monkey</li>
<li class="animals">Turtle</li>
<li class="animals">Whale</li>
<li class="animals">Aligator</li>
<li class="animals">Donkey</li>
<li class="animals">Horse</li>
</ol>
<!-- linking javscript -->
<script src="./animals.js"></script>
</body>
</html>
styles.css
#searchbar{
margin-left: 15%;
padding:15px;
border-radius: 10px;
}
input[type=text] {
width: 30%;
-webkit-transition: width 0.15s ease-in-out;
transition: width 0.15s ease-in-out;
}
/* When the input field gets focus,
change its width to 100% */
input[type=text]:focus {
width: 70%;
}
#list{
font-size: 1.5em;
margin-left: 90px;
}
.animals{
display: list-item;
}
animals.js
function search_animal() {
let input = document.getElementById('searchbar').value
input=input.toLowerCase();
let x = document.getElementsByClassName('animals');
for (i = 0; i < x.length; i++) {
if (!x[i].innerHTML.toLowerCase().includes(input)) {
x[i].style.display="none";
}
else {
x[i].style.display="list-item";
}
}
}
Comments
Leave a comment