Search code examples
jquerydom-traversal

Jquery traversing a table (Error: Not a function)


$(function(){
  $('.swaction').click(function(event){
    var num = $(this).parent().parent().children().first().children.first().val();
    alert(num);
    event.preventDefault();
  });
})

<table class="table-a" id="unrecognizedPDF">
  <thead>
    <tr>
      <th>Nombre de archivo</th>
      <th>Fecha de creación</th>
      <th>Acciones</th>
     </tr>
   </thead>
<tbody>
  <tr>
   <td>12313242122333</td>
   <td>07/06/2013 10:52:52</td>
   <td><a href="#" data-action="ver" class="swaction">Ver</a> |
       <a href="#" data-action="renombrar" class="swaction">Renombrar</a> |
       <a href="#" data-action="ver" class="swaction">Eliminar</a>
   </td>
  </tr>
 </tbody>
 </table>

I want to get this number: 12313242122333

I tried this: var num =

$(this).parent().parent().children().first().children.first().val();

but Firebug says it's not a function. Any clue on what is wrong? This is the first time I try to traverse the tree.

Thanks!

Error:

TypeError: $(...).parent(...).parent(...).children(...).first(...).children.first is not a function
http://192.168.100.196/printbox/js/js_procesar_escaneos.js
Line 3

Solution

  • Use var num = $(this).closest('tr').find('td:first').html(); instead.

    jsFiddle example

    .val() is for input elements, you want to get the HTML which you'd use .html() for.