I needed a quick searchable table and i was able to find one at w3schools, GREAT! but.... it only searches the first column. Can someone please help me figure out how to modify the script so that it searches both columns? Greatly appreciate any guidance or assistance.
Here is the code:
<!DOCTYPE html><html><head></head><body><inputtype="text"id="myInput"onkeyup="myFunction()"placeholder="Search for names.."title="Type in a name"><tableid="myTable"><trclass="header"><thstyle="width:60%;">Name</th><thstyle="width:40%;">Country</th></tr><tr><td>Alfreds Futterkiste</td><td>Germany</td></tr><tr><td>Berglunds snabbkop</td><td>Sweden</td></tr><tr><td>Island Trading</td><td>UK</td></tr><tr></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 queryfor(i =0; i < tr.length; i++){
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";}}}}</script></body></html>
Question
s0nic69
Hey guys,
I needed a quick searchable table and i was able to find one at w3schools, GREAT! but.... it only searches the first column. Can someone please help me figure out how to modify the script so that it searches both columns? Greatly appreciate any guidance or assistance.
Here is the code:
Here is the link to the page link
Link to comment
https://www.neowin.net/forum/topic/1338926-assistance-with-table-search/Share on other sites
2 answers to this question
Recommended Posts