I want to delete a line if this <td> line contains only B in my lower code?
Please check the code below. Here I delete the line if this line td contains the value "B". But the code below deletes all the lines.
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> +4
3 answers
To remove tr , where a td inside it contains ONLY "B"
var searchString = 'B'; $("#mytable tr td:contains('" + searchString + "')").each(function() { if ($(this).text() == searchString) { $(this).parent().remove(); } }); To delete only td , which contains a 'B'
parent() places the selector in tr , which is then removed, along with all td elements. You do not need to use parent() here.
$("#mytable tr td:contains('B')").remove(); +7
That should work. Not sure if this is the best regex, but it works. It will remove the <tr> that contains the <td> with only one uppercase B http://jsfiddle.net/mV4z3/
$('td').filter(function(){ return /^B$/.test($(this).text()); }).parent().remove(); 0