I have HTML which looks like this:
<table class="table">
<tbody id="number">
<tr class="is-first">delete</tr>
<tr>delete</tr>
<tr>delete</tr>
<tr>
<td>123456789</td>
<td class="is-number">1</td>
</tr>
<tr>delete</tr>
<tr>delete</tr>
<tr>
<td>123456789</td>
<td class="is-number">1</td>
</tr>
<tr>
<td>123456789</td>
<td class="is-number">1</td>
</tr>
<tr>delete</tr>
<tr>delete</tr>
<tr>delete</tr>
<tr>
<td>123456789</td>
<td class="is-number">1</td>
</tr>
<tr>delete</tr>
<tr>delete</tr>
</tbody>
</table>
It should be like this:
<table class="table">
<tbody id="number">
<tr class="is-first">delete</tr>
<tr>
<td>123456789</td>
<td class="is-number">1</td>
</tr>
<tr>delete</tr>
<tr>
<td>123456789</td>
<td class="is-number">1</td>
</tr>
<tr>
<td>123456789</td>
<td class="is-number">1</td>
</tr>
<tr>delete</tr>
<tr>
<td>123456789</td>
<td class="is-number">1</td>
</tr>
</tbody>
</table>
I want to remove entries row if contains specific string except one between rows. I need Javascript or jQuery code to solve this problem.
I tried this code but it removes all of the table.
$('.table tr:contains("delete")').parent().remove();
2
Answers
As others said in comment,first you need to put text inside
td
instead oftr
For your question,the quick way is to filter the
tr
that needs to be deleted,then we delete it one by oneThis answer assumes a correct table structure to start with. eg.
delete
should be wrapped in a<td>
element. With that out of the way, let’s get to the answer.You can select all
<tr>
elements that contains<td>delete</td>
with:If you only want to select
<tr>
elements that contains<td>delete</td>
, that directly follow another<tr>
element that contains<td>delete</td>
. You can do so using the adjacent sibling combinator (+
).