I'm trying to filter a table from an alphabetical <select>
input with jQuery.
I have first and last names in two columns of the table, and I'd like to filter the rows by either of these.
I have a select input set up as so:
<select>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
...
</select>
and I'd like to filter this table:
<tr>
<td>Joe</td>
<td>Schmoe</td>
<td>$2482.79</td>
<td>172.78.200.124</td>
<td>http://gmail.com</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>$2776.09</td>
<td>119.232.182.142</td>
<td>http://www.example.com</td>
</tr>
How would I go about filtering the table using jQuery?
If the id of your table is sel, and the table is tab, this will do the trick. Change eq(0) to change the column to look in. An empty value in the select box will re-show all trs.
This will work assuming you only have one select and one table that is stuctured like your example
I came up with this. Pretty similar to what Elzo came up with but it limits it to the first two columns of the table.
It doesn't ignore case and assumes you have one select and the only tr's on the page are the ones you want to filter.
EDIT Added an 'ALL' option to show rows again.
Try this. If necessary substitute y with $(y).
Edit
@SolutionYogi. You are right. I guess this line can be rewriten as:
Never tried that though.
EDIT 2
I tested this. I hope is elegant enough as well and I doesn't have more errors.
$("td:not(:contains('" + input_value + "'))").remove();
This is case sensitive ... what specifically are you attempting to filter on?