This is a short example on how to use jQuery's :contains
selector.
Suppose we have a table like this:
First name | Last name | Country | Dob | |
---|---|---|---|---|
Johny | Walker | johny.walker@whiskey.com | Scotland | 1/1/1920 |
nikhil | Vartak | nikhil.vartak@hotmail.co.in | india | 12/5/1986 |
Peter | James | james_peter@hotmail.com | germany | 5/10/1960 |
nikhil | Vartak | nikhil.vartak@hotmail.com | india | 11/27/1984 |
Peter | James | james_peter@hotmail.com | germany | 5/10/1960 |
jack | Daniel | j'daniels@whiskey.com | USA | 1/10/1846 |
nikhil | Vartak | nikhil.vartak@hotmail.co.in | india | 12/5/1986 |
Peter | James | james_peter@hotmail.com | germany | 5/10/1960 |
jack | Daniel | j'daniels@whiskey.com | USA | 1/10/1846 |
jack | Daniel | j'daniels@whiskey.com | USA | 1/10/1846 |
Mark | Anderson | anderson_m@gmail.com | canada | 2/29/1980 |
jack | Daniel | j'daniels@whiskey.com | USA | 1/10/1846 |
nikhil | Vartak | nikhil.vartak@hotmail.co.in | india | 12/5/1986 |
jack | Daniel | j'daniels@whiskey.com | USA | 1/10/1846 |
Peter | James | james_peter@hotmail.com | germany | 5/10/1960 |
If we have an input, we could use this code to filter the results:
function searchTable() {
var input = $('#search-input').val();
$('#data-table').find('tr:not(:contains(' + input + '))').addClass('d-none'); // hide non matches
$('#data-table').find('tr:contains(' + input + ')').removeClass('d-none'); // show mathing tables
}
© 2024 by Ryan Rickgauer