• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / HTML / How to Filter a List of Divs with a Text Input Bar Using Javascript

How to Filter a List of Divs with a Text Input Bar Using Javascript

December 13, 2020 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  jQuery rotate – How to Rotate an Image using jQuery
  • 2.  React Axios Interceptor to Prevent Infinite Loops in JWT Authentication
  • 3.  Get the Sum of Array in JavaScript
  • 4.  Using JavaScript to Rotate an Image
  • 5.  Using JavaScript to Square a Number
  • 6.  Using jQuery prepend to Insert Element As First Child
  • 7.  Using JavaScript to Check if a Number is Divisible by 2
  • 8.  Using JavaScript to Convert String to Boolean
  • 9.  Convert String to Array in JavaScript
  • 10.  Using jQuery to Move Element Before Another

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy