Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

How to filter a table based on a simple text input

$
0
0

I have been struggling with trying to filter a table based on a simple text input. It works on the first column but I also want the user to be able to filter by the second column. So I tried to add the var ts but that didn't. Any help would be much appreciated :-)

function search () {
var field = document.getElementById("searchField");
var filter = field.value.toUpperCase();
var table = document.getElementById('table');
var tr = table.getElementsByTagName('tr');
var i;
var txtValue;
var td;
var ts;
if (filter.length > 0)
{
    for (i = 0; i < tr.length; i++)
    {
        td = tr[i].getElementsByTagName("td")[0];
        ts = tr[i].getElementsByTagName("td")[1];
        if (td)
        {
            txtValue = td.textContent || td.innerText || ts.textContent || ts.innerText;
            if (txtValue.toUpperCase().indexOf(filter) > -1)
            {
                tr[i].style.display = "";
            }
            else
            {
                tr[i].style.display = "none";
            }
        }
    }
}

}


Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>