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

128
Vistas
jquery how to check near element with certain class exist?

I have two element inside <tr>:

<table>
<tbody>
<tr>
   <td><input type="text" class="form-control input-state" onclick="myfunction(this)"></td>
   <td><input type="text" class="form-control input-city" onclick="myfunction(this)"></td>
</tr>
</tbody>
</table>

in the myfunction I want to check if element with cartain class exist or not:

if($(thisElem).closest("tr").find(".input-city").length){
    console.log('true')
 }

but $(thisElem).closest("tr").find(".input-city").length is always 0 ! how do I check this element existance?

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

0

This is to show that the problem is due to the invalid html since tr should be wrapped inside a table like this demo shows:

function myfunction(target){
  if($(target).closest("tr").find(".input-city").length){
    console.log('true');
  }else{
    console.log('false');
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h1>Working</h1>

<table>
  <tr>
   <td><input type="text" class="form-control input-state" onclick="myfunction(this)"></td>
   <td><input type="text" class="form-control input-city" onclick="myfunction(this)"></td>
  </tr>
</table>

<h1>Not Working</h1>

<tr>
 <td><input type="text" class="form-control input-state" onclick="myfunction(this)"></td>
 <td><input type="text" class="form-control input-city" onclick="myfunction(this)"></td>
</tr>

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