Using jQuery to search HTML text and show or hide accordingly

In this we will see how we could search the required text in an HTML and only view what we have found. The idea is to search a pattern as user types it and then ignore its case sensitivity and search the text. Once found display those blocks of data accordingly.
Copy and paste below code to search text from HTML and show that div only.
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src=""></script>
<script type="text/javascript">
           //This will ignore case sensitivity of the test
            $.expr[":"].contains = $.expr.createPseudo(function(arg) {
                return function( elem ) {
                    return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
            $("#searchTextBox").keypress(function() {
              //This will chack the length of entered text
              if($("#searchTextBox").val().length > 0){
                var userSerarchField = $("#searchTextBox").val();
                //Will find and display only that div row
                $(".rows:contains('"+ userSerarchField +"')").css("display","block");
              } else {
<input type="text" id="searchTextBox">
<div class="divMain">
  <div class="rows rowOne">
    <h5>Test One</h5>
  <div class="rows rowTwo">
    <h5>Test Two</h5>
  <div class="rows rowThree">
    <h5>Test Three</h5>
  <div class="rows rowFour">
    <h5>Test Four</h5>

Add new comment