Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove HTML table row by value

Let’s say there is a table:

<table id="tblPotrawySkladniki">
  <tbody>
    <tr><td>banana</td><td>10</td></tr>
    <tr><td>orange</td><td>20</td></tr>
    <tr><td>raspberry</td><td>20</td></tr>
  </tbody>
</table>

I would like to remove entire row where i.e. cell1 = orange.
How can I do this using jquery?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Consider the following two examples. Example 1:

$(function() {
  $("#tblPotrawySkladniki > tbody td").each(function(i, el) {
    if ($(el).text() === "orange") {
      $(el).parent().remove();
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tblPotrawySkladniki">
  <tbody>
    <tr>
      <td>banana</td>
      <td>10</td>
    </tr>
    <tr>
      <td>orange</td>
      <td>20</td>
    </tr>
    <tr>
      <td>raspberry</td>
      <td>20</td>
    </tr>
  </tbody>
</table>

This first example gives you more control over how you compare or seek out the cell. For example, you could use:

$(el).text().trim().toLowerCase() === "orange"

This would help ensure a case insensitive search.

Example 2:

$(function() {
  $("#tblPotrawySkladniki > tbody td:contains('orange')").parent().remove();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tblPotrawySkladniki">
  <tbody>
    <tr>
      <td>banana</td>
      <td>10</td>
    </tr>
    <tr>
      <td>orange</td>
      <td>20</td>
    </tr>
    <tr>
      <td>raspberry</td>
      <td>20</td>
    </tr>
  </tbody>
</table>

The second example relies on the selector and if they do not match exactly, will not find it. It’s quick and uses less lines, yet may not always find the needle.

Each of these, in their own way, will target the Cell and remove the parent Row. See More:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading