I have a simple table that shows date in columns. Each <td> has a data attribute data-port I'd like to filter the results shown in the table, based on an option selected from a drop downlist.
The HTML is :
<select id='filter'>
<option></option>
<option value='fd18'>OPTION A</option>
<option value='af13'>OPTION B</option>
</select>
<table id='table1'>
<tbody>
<tr>
<td data-port="fd18"><div id="123" class="title">123</div></td>
<td data-port="af13"><div id="456" class="title">456</div></td>
<td data-port="0000"><div id="789" class="title">789</div></td>
<td data-port="fd18"><div id="213" class="title">213</div></td>
</tr>
<tr>
<td data-port="fd18"><div id="345" class="title">345</div></td>
<td data-port="af13"><div id="946" class="title">946</div></td>
<td data-port="0000"><div id="368" class="title">368</div></td>
<td data-port="af13"><div id="246" class="title">246</div></td>
</tr>
<tr>
<td data-port="af13"><div id="642" class="title">642</div></td>
<td data-port="fd18"><div id="759" class="title">795</div></td>
<td data-port="fd18"><div id="335" class="title">335</div></td>
<td data-port="fd18"><div id="556" class="title">556</div></td>
</tr>
</tbody>
</table>
Using jquery I'm trying to show the td's where the data-port equals the selected option and hide the rest. How can I do this and it is it possible to dynamically update the table layout so I still end up with neat columns.
The jquery I'm using is :
$('#filter').change(function() {
var port = $(this).val()
console.log( port )
$("td > [data-port!=" + port+ "]").hide();
$("td > [data-port=" + port+ "]").show();
});
The console shows the correct value being returned, but I end up hiding all TD's on the page not just the ones which don't match
Any ideas how I can do this. Thanks