Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to remove the row if that row's <td> contains only B in my below code?

Tags:

jquery

Please check the below code. Here i'm deleting the row, if that row td contains the value "B". But bellow code is removing all rows.

jQuery

$(document).ready(function() {
    $("#mytable tr td:contains('B')").parent().remove();
});

HTML

<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>
like image 588
sandeep Avatar asked Feb 20 '12 09:02

sandeep


2 Answers

To remove the tr where a td within it contains ONLY B

var searchString = 'B';
$("#mytable tr td:contains('" + searchString + "')").filter(function() {
  return $(this).text().trim() == searchString;
}).parent().remove();

Example fiddle

To remove only the td which contains a B:

The parent() is putting the selector on the tr, which is then being removed - along with all td elements. You don't need to use parent() here.

$("#mytable tr td:contains('B')").remove();

Example fiddle

like image 138
Rory McCrossan Avatar answered Sep 22 '22 13:09

Rory McCrossan


Sandeep Try this.

     $(document).ready(function()
     {
        $("#mytable td").text(function(index, currentText) 
        {
            if(currentText.trim()=='B')
                $(this).parent().remove();
        }); 
    });
like image 27
Hearaman Avatar answered Sep 21 '22 13:09

Hearaman