Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
Requesting text from a table row using jquery returns ''

I started using jQuery around a week ago and I wanted to make something like a search from an existing table.

When calling text() from any row it shows and empty string '' making it impossible to compare the searched string to the existing ones in the table.

For example, the following code returns 4 instances of ''. How can I fix this?

$(document).ready(() => {
  $('#test tr').filter(() => {
    console.log($(this).text());
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table>
  <tbody id="test">
    <tr>
      <td>example</td>
      <td>1</td>
    </tr>
    <tr>
      <td>example</td>
      <td>2</td>
    </tr>
    <tr>
      <td>example</td>
      <td>3</td>
    </tr>
    <tr>
      <td>example</td>
      <td>4</td>
    </tr>
  </tbody>
</table>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There's two problems in your code. Firstly you're using an arrow function so the this keyword will point to the outer context, not the tr element within the iteration. To fix this use an anonymous function, or receive the tr as the argument to the arrow function. I've done the latter in the example below.

The second issue is that you're using filter() as an iterator, which is not correct. filter() should be used to reduce a set of elements within a jQuery object. As you want to loop in this case, you should just use each() instead.

jQuery($ => {
  $('#test tr').each((i, tr) => {
    console.log($(tr).text().trim());
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table>
  <tbody id="test">
    <tr>
      <td>example</td>
      <td>1</td>
    </tr>
    <tr>
      <td>example</td>
      <td>2</td>
    </tr>
    <tr>
      <td>example</td>
      <td>3</td>
    </tr>
    <tr>
      <td>example</td>
      <td>4</td>
    </tr>
  </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda