1

I have a table and I need to remove all rows that don't contain a string.

$(document).ready(function() {
    var str = "b";
    $("#mytable tr td:not(:contains(str))").parent().remove();
});
//this doesn't work
<table border="0" align="center" width="45%" cellpadding="2" cellspacing="2"  id="mytable">
    <tr>
        <td align="center" width="15%">A</td>
        <td align="center" width="15%">B</td>
        <td align="center" width="15%">C</td>
    </tr>
    <tr>
        <td align="center" width="15%">AA</td>
        <td align="center" width="15%">BB</td>
        <td align="center" width="15%">CC</td>
    </tr>
    <tr>
        <td align="center" width="15%">AAA</td>
        <td align="center" width="15%">BBB</td>
        <td align="center" width="15%">CCC</td>
    </tr>
</table>

I need to only keep the rows where the td text exactly matches the string. What i'm getting is either all rows being deleted, no rows being deleted, or rows being deleted except those that contain "B", "BB", or "BBB". Perhaps "contains" isn't the correct way to do this?

digerati
  • 83
  • 7
  • Contains will return true if the text value is found, this could be an exact match but isn't always the case. If you need to remove based on exact match, you'll probably want to iterate each cell in the row and check to see if an exact match is found, if so, move to the next row, else remove the row. – Ryan Wilson Dec 01 '22 at 15:38
  • All of your row contains td which had string in it – Nexo Dec 01 '22 at 15:39
  • @Nikkkshit I think the OP is looking for a particular string value, not `typeof string`. – Andy Dec 01 '22 at 15:45
  • `$("#mytable tr td:not(:contains(str))")` should be `$("#mytable tr td:not(:contains(" + str + "))")` at a minimum. – freedomn-m Dec 01 '22 at 15:56
  • Does this answer your question? [How to use variables in a jquery selector](https://stackoverflow.com/questions/5891840/how-to-use-javascript-variables-in-jquery-selectors) – freedomn-m Dec 01 '22 at 15:57
  • check this https://codepen.io/RamiAlmofleh/pen/GRGYNdg?editors=1010 – Rami Almofleh Dec 01 '22 at 16:02

1 Answers1

0

As stated in the comments, if you want to do an exact match, then contains is not the way to go, instead you will want to check the cells text for an exact match of each row:

$(document).ready(function() {
    checkCells($('#mytable'),"B");
});

//made this a function so it can be re-used in other places
function checkCells(table, text){
    const $table = $(table); //set parameter to jQuery variable
    //iterate the rows in the table
    $table.find("tr").each((i, el) => {
         let found = false;
         //check each cell for the exact text
         $(el).find('td').each((i, t) => {
              if($(t).text() === text) {
                  found = true;
                  return false;
              }
         });
         //if the text isn't found in a cell, remove the row
         if(!found){
             $(el).remove();
         }
    });
}
<script
  src="https://code.jquery.com/jquery-3.6.1.js"
  integrity="sha256-3zlB5s2uwoUzrXK3BT7AX3FyvojsraNFxCc2vC/7pNI="
  crossorigin="anonymous"></script>
<table border="0" align="center" width="45%" cellpadding="2" cellspacing="2"  id="mytable">
    <tr>
        <td align="center" width="15%">A</td>
        <td align="center" width="15%">B</td>
        <td align="center" width="15%">C</td>
    </tr>
    <tr>
        <td align="center" width="15%">AA</td>
        <td align="center" width="15%">BB</td>
        <td align="center" width="15%">CC</td>
    </tr>
    <tr>
        <td align="center" width="15%">AAA</td>
        <td align="center" width="15%">BBB</td>
        <td align="center" width="15%">CCC</td>
    </tr>
</table>
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
  • This worked perfectly, thank you so much! I have no idea why this was closed as a "duplicate question", as none of those other questions in the links are similar to this one. :/ – digerati Dec 01 '22 at 16:07
  • @digerati As far as the "duplicate question", it tends to happen a lot. Don't get discouraged. The community here is not what I would consider as welcoming or even friendly, if it gets to be too much, there are other coding sites out there that are much more welcoming. [alternatives-of-stack-overflow](https://aspiringcoders.com/alternatives-of-stack-overflow/) – Ryan Wilson Dec 01 '22 at 16:17
  • *none of those other questions in the links are similar* - you stated in your question: "*I need to only keep the rows where the td text **exactly matches** the string. I'm getting either all, no rows being deleted, or except those with B"* - this can be fixed using either a `.filter` or an extension to jquery's selectors. Each of the provided answers explains this in detail, so doesn't need to be repeated. With an extension, your code becomes a single-liner `$("#mytable tr").filter((i, e) => !$(e).find("td:containsExact(" + str + ")").length).remove();` https://jsfiddle.net/gomvz1t5/ enjoy:) – freedomn-m Dec 01 '22 at 16:42
  • Thank you for your reply. After having searched the interwebs (including stack overflow) for hours, using many many variations of my question and none of those linked questions being returned, I finally decided to ask specifically. Perhaps someone else in the same situation will be able to benefit. Not all of us are JS masters, most are just learning. Having simple code samples and specific answers are hugely helpful to those of us who are learning. Since most of my questions are replied to in a harsh manner here, I will probably look into the alternatives @RyanWilson helpfully offered. – digerati Dec 01 '22 at 16:57