Is there a way to search across multiple tables in HTML? I currently have code that works for one table, but I need it to work for two or more tables.
This is the code I am using to search for "something" within my table.
<script>
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
if (!tr[i].classList.contains('header')) {
td = tr[i].getElementsByTagName("td"),
match = false;
for (j = 0; j < td.length; j++) {
if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
match = true;
break;
}
}
if(!match){
tr[i].style.display = "none";
}else{
tr[i].style.display = "";
}
}
}
}
</script>
This is the structure of the HTML Tables where I want to implement the search functionality.
The issue is that the search functionality only runs on the first table and not on the subsequent ones.
<table class="table" style="text-align: left;" id="myTable">
<thead>
<tr>
</tr>
</thead>
<tbody>
<h4 style="text-align: center;"><strong>FOREIGN MINISTER’S
OFFICE</strong></h4>
<div id="A">
<tr>
<td><strong><h5>Designation</strong></h5></td>
<td><strong><h5>Name</strong></h5></td>
<td><strong><h5>Phone</strong></h5></td>
<td><strong><h5>Fax</strong></h5></td>
</tr>
<tr>
<td>Foreign Minister</td>
<td>Makhdoom Shah Mahmood Qureshi</td>
<td>051-9210335</td>
<td>051-9207600</td>
</tr>
<tr>
<td></td>
<td></td>
<td>051-9203824</td>
<td></td>
</tr>
<tr>
<td>Additional Secretary (FMO)</td>
<td>Ameer Khurram Rathore</td>
<td>051-9210335</td>
<td></td>
</tr>
<tr>
<td>Director (FMO)</td>
<td>Syed Mustafa Rabbani</td>
<td>051-9207762</td>
<td></td>
</tr>
<tr>
<td>Asstt. Dir (FMO)</td>
<td>Muhammad Saad Ahmed</td>
<td>051-9207617</td>
<td></td>
</tr>
<tr>
<td>PS to FM</td>
<td>Muhammad Bashir Kiyani</td>
<td>051-9207762</td>
<td></td>
</tr>
</div>
</tbody>
</table>
<table class="table" style="text-align: left;" id="myTable">
<thead>
<tr>
</tr>
</thead>
<tbody>
<h4 style="text-align: center;"><strong>PARLIAMENTARY SECRETARY’S OFFICE</strong></h4>
<div id="B">
<tr>
<td><strong><h5>Designation</strong></h5></td>
<td><strong><h5>Name</strong></h5></td>
<td><strong><h5>Phone</strong></h5></td>
<td><strong><h5>Fax</strong></h5></td>
</tr>
<tr>
<td>Foreign Minister</td>
<td>Makhdoom Shah Mahmood Qureshi</td>
<td>051-9210335</td>
<td>051-9207600</td>
</tr>
<tr>
<td></td>
<td></td>
<td>051-9203824</td>
<td></td>
</tr>
<tr>
<td>Additional Secretary (FMO)</td>
<td>Ameer Khurram Rathore</td>
<td>051-9210335</td>
<td></td>
</tr>
<tr>
<td>Director (FMO)</td>
<td>Syed Mustafa Rabbani</td>
<td>051-9207762</td>
<td></td>
</tr>
<tr>
<td>Asstt. Dir (FMO)</td>
<td>Muhammad Saad Ahmed</td>
<td>051-9207617</td>
<td></td>
</tr>
<tr>
<td>PS to FM</td>
<td>Muhammad Bashir Kiyani</td>
<td>051-9207762</td>
<td></td>
</tr>
</div>
</tbody>
How can I enable searching across multiple tables in an HTML environment?