Filter Table and List easily using Bootstrap 4

Yaksh Bhesaniya
4 min readMay 9, 2020

--

You can create Filterable Table, List, Paragraphs, Buttons, Etc.. using Bootstrap.

Bootstrap is a framework which contains libraries of HTML, CSS and JS.

If you’re new to Bootstrap or you don’t know what really it is.. I recommend you to go to my this article to get the entire basic idea of Bootstrap framework.

Bootstrap doesn’t have a component that permits filtering. However, we will use jQuery to filter / look for elements.

For Filter Tables, List, Paragraphs, Buttons, etc.. you need to use jQuery.

<script>
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>

Filter Tables

Here it is the source code for performing a search for items in a table.

<!DOCTYPE html>
<html lang="en">
<head>
<title>...</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h2>Filterable Table</h2>
<p>Type something in the input field to search the table for Names, Countries or emails:</p>
<input class="form-control" id="myInput" type="text" placeholder="Search..">
<br>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Country</th>
<th>Email</th>
</tr>
</thead>
<tbody id="myTable">
<tr>
<td>ABC</td>
<td>India</td>
<td>abc90@gmail.com</td>
</tr>
<tr>
<td>PQR</td>
<td>US</td>
<td>pqr12@gmail.com</td>
</tr>
<tr>
<td>XYZ</td>
<td>UK</td>
<td>xyz73@greatstuff.com</td>
</tr>
</tbody>
</table>

</div>
<script>
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>
</body>
</html>

Before typing something in the input field:

After typing something in the input field:

Filter Lists

Here it is the source code for performing a search for items in a list.

<!DOCTYPE html>
<html lang="en">
<head>
<title>...</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h2>Filterable List</h2>
<p>Type something in the input field to search the list for specific items:</p>
<input class="form-control" id="myInput" type="text" placeholder="Search..">
<br>
<ul class="list-group" id="myList">
<li class="list-group-item">First item</li>
<li class="list-group-item">Second item</li>
<li class="list-group-item">Third item</li>
<li class="list-group-item">Fourth</li>
</ul>
</div>
<script>
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myList li").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>
</body>
</html>

Before typing something in the input field:

After typing something in the input field:

Filter Paragraphs, Buttons, Etc..

Here it is the source code for performing a search for items in a <div>.

<!DOCTYPE html>
<html>
<head>
<title>...</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h2>Filter Paragraphs, Buttons, Etc..</h2>
<p>Type something in the input field to search for a text inside the div element</p>
<input class="form-control" id="myInput" type="text" placeholder="Search..">
<div id="myDIV" class="mt-3">
<p>I am a boy.</p>
<div>I am a girl.</div>
<button class="btn">I am a button1</button>
<button class="btn btn-info">I am a button2</button>
<p>Another paragraph.</p>
</div>
</div>
<script>
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myDIV *").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>
</body>
</html>

Before typing something in the input field:

After typing something in the input field:

I hope you’ve found this article informative and helpful. I’d love to hear your feedback!

Thanks for reading! :)

--

--

Yaksh Bhesaniya

Enthusiastic learner and programmer who loves sharing his knowledge and experiences.