Create Search box / search list in a webpage using unordered list 




We have already created a search box using unordered list with showing list below. Now we are going to create a search box without showing the search list. The code given below creates a search box without showing the search list and shows the search list only when the cursor focuses on the search box.





As in the video , it does not show any list below in the search box but when we focuses the cursor into the search box, the list get displayed and searched the term we want. We can add any number of list below the search bar.

Code:



<!doctype html>
<style>
#myUL,#myInput {
 box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 3px solid black;
  margin-bottom: 12px;
border-radius:10px;

}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
display:none;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color:#f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block;
}
#myInput:focus + #myUL
{
display:block;


}
#myUL li a:hover
{
display:block;
background-color:lightblue;
}
#myUL:hover
{
display:block;
}
</style>



<div id="divtest">

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name" />

<ul id="myUL">


 <li><a href="#">java</a></li>

  <li><a href="#">c</a></li>

  <li><a href="#">css</a></li>
  <li><a href="#">c++</a></li>

  <li><a href="#">python</a></li>
  <li><a href="#">sql</a></li>
  <li><a href="#">julia</a></li>

</ul>

</div>
<script>
function myFunction() {
    var input, filter, ul, li, a, i, txtValue;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) {
        a = li[i].getElementsByTagName("a")[0];
        txtValue = a.textContent || a.innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";
        }
    }
}
</script>


</!doctype>

Comments

Popular posts from this blog

Node.js Cheat Sheet

Codeigniter ! Simple But Powerful

Introducing Cloudflare Pages ⚡