I am trying to get the closest table when I search for a value.
This is my code but I always get 'undefined'.
jQuery :
$(function() {
$("#searchValue").keyup(function() {
alert($(this).closest('table').attr('id'));
});
});
HTML:
<input type='text' name='searchValue' id='searchValue'/>
<table id='tablePlanning' class='tablesorter'>
<thead>
<tr>
<th>PR Code</th>
<th>Klant</th>
<th>Description</th>
<th>Project status</th>
<th>Project Leader</th>
<th>Coordinator</th>
<th>Account manager</th>
<th>Billing</th>
<th>Start Datum</th>
<th>Hardware</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Try to use .next()
to find the following table
(at the same DOM level) instead :
$(function() {
$("#searchValue").keyup(function() {
alert($(this).next('table').attr('id'));
});
});
.closest()
is to find the first parent of the element.
But since your table has an id
, you should use it (I hope you don't have several tables with the id #tablePlanning
otherwise, you should use a classname.