Created
January 27, 2017 11:27
-
-
Save pawiromitchel/dead1afdd6ee6375767930e0e2fde766 to your computer and use it in GitHub Desktop.
JS - search within a table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
$("#search").keyup(function () { | |
var value = this.value.toLowerCase().trim(); | |
$("table tr").each(function (index) { | |
if (!index) return; | |
$(this).find("td").each(function () { | |
var id = $(this).text().toLowerCase().trim(); | |
var not_found = (id.indexOf(value) == -1); | |
$(this).closest('tr').toggle(!not_found); | |
return not_found; | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<strong>Search Here</strong> | |
<input type="text" id="search" class="form-control" placeholder="Search" data-original-title="" title=""> | |
<br> | |
<table class="table table-bordered"> | |
<thead> | |
<tr> | |
<th>Header 1</th> | |
<th>Header 2</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<td>Data 1_1</td> | |
<td>Data 1_2</td> | |
</tr> | |
<tr> | |
<td>Data 2_1</td> | |
<td>Data 2_2</td> | |
</tr> | |
</tbody> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment