Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
jquery ¿cómo comprobar que existe un elemento cercano con cierta clase?

Tengo dos elementos dentro <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>

en myfunction quiero verificar si el elemento con la clase cartain existe o no:

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

pero $(thisElem).closest("tr").find(".input-city").length es siempre 0 ! ¿Cómo verifico la existencia de este elemento?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Esto es para mostrar que el problema se debe a que el html no es válido, ya que tr debe estar dentro de una tabla como muestra esta demostración:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!