search table javascript code example
Example 1: search in a table 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>
Example 2: search functionality for html table
BY LOVE SINGH,
Call this function onKeyUp event of TEXTBOX.
function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("TextBoxID");
filter = input.value.toUpperCase();
table = document.getElementById("TableID");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[1];
var x = $("#TextBoxID").val();
var regex = /^[a-zA-Z]+$/;
if (!x.match(regex)) {
td = tr[i].getElementsByTagName("td")[0];
}
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
Example 3: search functionality for html table on text box
BY LOVE SINGH,
Call this function onKeyUp event of TEXTBOX.
function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("TextBoxID");
filter = input.value.toUpperCase();
table = document.getElementById("TableID");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[1];
var x = $("#TextBoxID").val();
var regex = /^[a-zA-Z]+$/;
if (!x.match(regex)) {
td = tr[i].getElementsByTagName("td")[0];
}
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
Example 4: how to search through a table javascript
(function() {
'use strict';
var TableFilter = (function() {
var Arr = Array.prototype;
var input;
function onInputEvent(e) {
input = e.target;
var table1 = document.getElementsByClassName(input.getAttribute('data-table'));
Arr.forEach.call(table1, function(table) {
Arr.forEach.call(table.tBodies, function(tbody) {
Arr.forEach.call(tbody.rows, filter);
});
});
}
function filter(row) {
var text = row.textContent.toLowerCase();
var val = input.value.toLowerCase();
row.style.display = text.indexOf(val) === -1 ? 'none' : 'table-row';
}
return {
init: function() {
var inputs = document.getElementsByClassName('table-filter');
Arr.forEach.call(inputs, function(input) {
input.oninput = onInputEvent;
});
}
};
})();
TableFilter.init();
})();