2

I've removed table rows with

jQuery('td:contains(No)').parent().hide();

But cells that contain's a word that begin with "No" will also removed.

This is my rendered html code:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="data-table" id="product-attribute-specs-table">
     <col width="25%" />
     <col />
     <tbody>
       <tr>
          <th class="label">Zusatz-Info</th>
          <td class="data">No</td>
       </tr>
       <tr>
           <th class="label">Info</th>
           <td class="data">Nothing</td>
       </tr>
     </tbody>
  </table>
<script type="text/javascript">
  jQuery('td:contains(No)').parent().hide();
</script>

How can I select only the td's that contains the No?

Thanks for help a jQuery newbie.

Mamun
  • 66,969
  • 9
  • 47
  • 59
Tyv
  • 29
  • 4

2 Answers2

0

You can not that directly. One possible solution is to use data- attribute and Attribute Equals Selector like the following way:

jQuery('td[data-flag=No').parent().hide();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="data-table" id="product-attribute-specs-table">
   <col width="25%" />
   <col />
   <tbody>
     <tr>
        <th class="label">Zusatz-Info</th>
        <td class="data" data-flag="No">No</td>
     </tr>
     <tr>
         <th class="label">Info</th>
         <td class="data" data-flag="Nothing">Nothing</td>
     </tr>
   </tbody>
</table>
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

 $(document).ready(function() {
    $("td").filter(function() {
      if($(this).text() === "No") {
       $(this).parent().hide();
      }
   });
 });
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
  <table class="data-table" id="product-attribute-specs-table">
   <col width="25%" />
   <col />
   <tbody>
     <tr>
        <th class="label">Zusatz-Info</th>
        <td class="data" data-flag="No">No</td>
     </tr>
     <tr>
         <th class="label">Info</th>
         <td class="data" data-flag="Nothing">Nothing</td>
     </tr>
   </tbody>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>
</html>

Try this:

 $(document).ready(function() {
    $("td").filter(function() {
      if($(this).text() === "No") {
        // remove text
        $(this).text('');
        // or hide tr
        $(this).parent().hide();
      }
    });
 });
Fiodorov Andrei
  • 1,778
  • 1
  • 11
  • 26
  • Hello Fiodorov Andrei,I don't understand your "$" sign. I use jQuery at the beginning. Why you use $? The code I use is from Magento 1.9 Shop - there is also prototype.js, scriptaculous,js and jquery.js. Can you explain where I'm should use jQuery or $? – Tyv Sep 14 '19 at 08:36
  • Hello. My solution based on html without data attribute. – Fiodorov Andrei Sep 14 '19 at 08:37
  • This is what I get in Console: **Uncaught TypeError: $(...).text is not a function at HTMLTableCellElement. (.html:727) at jquery-1.9.1.min.js:4** – Tyv Sep 14 '19 at 08:46
  • try this: ```$(document).ready(function() { $("td").filter(function(index, cell) { if(cell.innerHTML === "No") { $(this).parent().hide(); } }); });``` – Fiodorov Andrei Sep 14 '19 at 08:59
  • Online code [example](https://jsfiddle.net/r3jynhw8/4/) – Fiodorov Andrei Sep 14 '19 at 09:02