Question

$(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
Was it helpful?

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.

OTHER TIPS

You can't use .val() on td element, it's for form element. try .text()

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

You're trying to call the first() method of the children function.

You need to call the children function using parentheses.

$(this).closest('tr').find('td:first').html();

Try this.

Use html instead of val

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top