Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda