How to find closest table jQuery

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>

Answers


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.


Need Your Help

Reverse list of objects inside a list of object using linq

vb.net asp.net-mvc-3 linq linq-to-objects

I've a list of class type hotel which internally consists list of other class rooms. Now when i perform a descending sort i sort hotels in descending order but i also have to internally sort the ro...

jQuery Mobile changePage()

php javascript jquery redirect

From this.php, if you click a button, changePage(#here) will execute.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.