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

165
Views
Necesito verificar si algún <td> en una tabla html está vacío/nulo, ya que después de cargar la página solo devuelve <td></td> y cámbielo a <td><p>$0</p></ td>

ejemplo de la tabla

 <table class="tg"> <thead> <tr> <th class="tg-0lax" id="blank-spaces"></th> <th class="titles" id="this">????</th> <th class="titles">???<br></th> <th class="titles">???</th> <th class="titles">???</th> </tr> </thead> <tbody> <tr> <td></td> <td>not empty do nothing</td> <td></td> </tr> </tbody> <table>

Ahora, de la forma en que esto está realmente escrito, los datos se insertarán en cada td desde una API, algunas veces esa API está inactiva, y me gustaría usar jquery para verificar si un td tiene algo que mostrar y si no es así, quiero allí ser una cadena con un mensaje de error en el td. Este es el jquery que estoy intentando actualmente

 var empty = $("td").trim().filter(function () { return this.value.trim() === null }) empty.addClass("errorDefault"); if ($("td").hasClass("errorDefault")) { this.val("$0"); this.text("$0"); this.html("<p>There was an error getting data</p>"); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • No hay .trim() en jQuery
  • string trim() no devolverá un valor nulo.
  • las celdas de la tabla no tienen valor
  • $("td").hasClass("errorDefault") solo mira el primer elemento

 $("tbody td") .filter((_, td) => !td.textContent.trim().length) .addClass("errorDefault") .text("$0");
 .errorDefault { background-color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="tg"> <thead> <tr> <th class="tg-0lax" id="blank-spaces"></th> <th class="titles" id="this">????</th> <th class="titles">???<br></th> <th class="titles">???</th> <th class="titles">???</th> </tr> </thead> <tbody> <tr> <td></td> <td>not empty do nothing</td> <td></td> </tr> </tbody> <table>

Si está realmente vacío, CSS puede hacerlo.

 tbody td:empty{ background: red; } tbody td:empty:after { content: "$0"; }
 <table class="tg"> <thead> <tr> <th class="tg-0lax" id="blank-spaces"></th> <th class="titles" id="this">????</th> <th class="titles">???<br></th> <th class="titles">???</th> <th class="titles">???</th> </tr> </thead> <tbody> <tr> <td></td> <td>not empty do nothing</td> <td></td> </tr> </tbody> <table>

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!