What is the best way to search all the columns in my HTML table? Currently, my table only searches the first column.
Additionally, my table has multiple header rows. I am not sure what is the best way to display search results if the results are only for certain header sections. Only the related headers should be displayed with the search results.
https://codepen.io/magic12/pen/QWmvGBX
function myFunction() {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
table.table_brdr td {
padding: 8px 10px;
border: none;
}
table.table_brdr th {
background-color: #a6a6a6;
color: black;
}
tr:nth-of-type(odd) {
background-color#D3D3D3;
}
#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 20%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
<p>
<input
type="text"
id="myInput"
onkeyup="myFunction()"
placeholder="Search"
title="Searche"
/>
</p>
<table class="table_brdr" id="myTable">
<tbody>
<tr>
<th>Column1</th>
<th><strong>Column2</strong></th>
<th>Column3</th>
</tr>
<tr>
<td>abc</td>
<td>xyz</td>
<td>03/30/2017</td>
</tr>
<tr>
<td>test12</td>
<td>https://www.yahoo.com/</td>
<td>03/30/2017</td>
</tr>
<tr>
<th>Column1</th>
<th>New Column</th>
<th>Column3</th>
</tr>
<tr>
<td>John</td>
<td>abctd</td>
<td>09/30/2019</td>
</tr>
<tr>
<th>Column1</th>
<th>New Column2</th>
<th>Column3</th>
</tr>
<tr>
<td>Doe</td>
<td>abctd</td>
<td>06/30/2019</td>
</tr>
</tbody>
</table>