How to hide a table row that the information contains *

Asked

Viewed 60 times

1

I have a table that when you don’t have information it’s filled with a * for example:

<tr>
   <td>Quant. do produto</td>
   <td>*</td>
</tr>

But the same can appear with information:

<tr>
   <td>Quant. do produto</td>
   <td>12</td>
</tr>

How would I hide what is without information (owning the *) dynamically?

Note: if necessary, I can remove the * and leave simply empty .

2 answers

4

Just go through all the td's and remove the line from td with only the asterisk:

document.addEventListener("DOMContentLoaded", function(){
   // seleciona todas as td's da tabela
   var tab = document.querySelectorAll("table td");
   for(var x=0; x<tab.length; x++){
      var td = tab[x];
      if(td.textContent.trim() == "*"){
         // remove a linha
         td.parentNode.outerHTML = '';
      }
   }
});
<table>
   <tr>
      <td>Quant. do produto</td>
      <td>*</td>
   </tr>
   <tr>
      <td>Quant. do produto</td>
      <td>12</td>
   </tr>
   <tr>
      <td>Quant. do produto</td>
      <td>*</td>
   </tr>
</table>

  • You can make it happen automatically?

  • Yes, you do. I changed the answer. ;)

  • Vlw for the help friend ;)

1


I believe that using Jquery would be more practical like this:

$('tr:has(td:contains("*"))').hide();

  • 1

    But then you’re not hiding the line, just the cell.

  • Makes sense your remark, I made an adjustment to pick up by the <tr>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.