Being able to add filtering functionality to lists using Javascript and jQuery can be very beneficial for the user experience of a web page.

It is very common for a page to have a list of products, events, blogs, etc. which the user could want to sort or filter – depending on the purpose of the web page and the information you are trying to present.

In any case, being able to dynamically update the page using Javascript or jQuery can be very beneficial.

One way to be able to add filtering to a web page is to use a list of divs, add data attributes to each div, and then filter using text input from a text bar input.

For example, let’s say you have a list of divs with some data attributes:

<div id="list-of-divs">
   <div class="div" data-content="frog">frog</div>
   <div class="div" data-content="elephant">elephant</div>
   <div class="div" data-content="giraffe">giraffe</div>
   <div class="div" data-content="alligator">alligator</div>
</div>

In this example, we want to use a text input to control the filtering functionality of these divs. Creating an input is easy, and we need to also add the attribute onkeyup and pass it the function that we will use to update the list of divs:

<div class="input-container">
   <span class="input-text">Filter:</span>
   <input id="myInput" type="text" onkeyup="filterTextInput()" />
</div>

To filter the list of divs, what we need to do is the following:

  • Get the text input
  • Get the list of divs
  • Loop through the divs and see if the input text is in the data attribute of the div
  • Set the display of divs which do not contain the input text to “none”

Below is the Javascript code which can accomplish the filtering of a list of divs with a text input:

function filterTextInput() {
  var input, radios, radio_filter, text_filter, td0, i, divList;
  input = document.getElementById("myInput");
  text_filter = input.value.toUpperCase();
  divList = $(".div");
	
 // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < divList.length; i++) {
    td0 = divList[i].getAttribute('data-content');
    if (td0) {
      if (td0.toUpperCase().indexOf(text_filter) > -1) {
        divList[i].style.display = "";
      } else {
        divList[i].style.display = "none";
      }
    } 
  }
}

If you are using WordPress, don’t forget to change the $ to jQuery as below:

function filterTextInput() {
  var input, radios, radio_filter, text_filter, td0, i, divList;
  input = document.getElementById("myInput");
  text_filter = input.value.toUpperCase();
  divList = jQuery(".div");
	
 // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < divList.length; i++) {
    td0 = divList[i].getAttribute('data-content');
    if (td0) {
      if (td0.toUpperCase().indexOf(text_filter) > -1) {
        divList[i].style.display = "";
      } else {
        divList[i].style.display = "none";
      }
    } 
  }
}

The final code and output for this example of how to use a text input to filter a list of divs using Javascript and jQuery is below:

Code Output:

Filter:
frog
elephant
giraffe
alligator

Full Code:

<div class="input-container">
   <span class="input-text">Filter:</span> 
   <input id="myInput" type="text" onkeyup="filterTextInput()" />
</div>
<div id="list-of-divs">
   <div class="div" data-content="frog">frog</div>
   <div class="div" data-content="elephant">elephant</div>
   <div class="div" data-content="giraffe">giraffe</div>
   <div class="div" data-content="alligator">alligator</div>
</div>

<script>

function filterTextInput() {
  var input, radios, radio_filter, text_filter, td0, i, divList;
  input = document.getElementById("myInput");
  text_filter = input.value.toUpperCase();
  divList = $(".div");
	
 // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < divList.length; i++) {
    td0 = divList[i].getAttribute('data-content');
    if (td0) {
      if (td0.toUpperCase().indexOf(text_filter) > -1) {
        divList[i].style.display = "";
      } else {
        divList[i].style.display = "none";
      }
    } 
  }
}

</script>

Hopefully this article has been very useful for you to be able to add the functionality if filtering a list of divs with a text input bar.

Categorized in:

HTML, JavaScript, jQuery,

Last Update: March 21, 2024