I need to delete table rows in range from row with an ID "#deleteFirstRow" to row with an ID "#deleteLastRow". But all the other rows out of these two IDs should remain.
NB: I tried to search posted answers but did not get any working solution. Please help!
This is my table:
<table>
<tr>doNotDeleteRow</tr>
<tr>doNotDeleteRow</tr>
<tr>doNotDeleteRow</tr>
<tr id="deleteFirstRow">deleteRow</tr>
<tr>deleteRow</tr>
<tr>deleteRow</tr>
<tr>deleteRow</tr>
<tr id="deleteLastRow">deleteRow</tr>
<tr>doNotDeleteRow</tr>
<tr>doNotDeleteRow</tr>
<tr>doNotDeleteRow</tr>
</table>
I tried this code:
$("table tr:not(#deleteFirstRow, #deleteLastRow)").remove();
But this code removes all rows except the ones with IDs: "#deleteFirstRow" and "#deleteLastRow".
What I want is to remove only rows between "#deleteFirstRow" and "#deleteLastRow" and also remove "#deleteFirstRow" and "#deleteLastRow".
So the expected result should be like below:
<table>
<tr>doNotDeleteRow</tr>
<tr>doNotDeleteRow</tr>
<tr>doNotDeleteRow</tr>
<tr>doNotDeleteRow</tr>
<tr>doNotDeleteRow</tr>
<tr>doNotDeleteRow</tr>
</table>
2
Answers
You can
slice
the collection of all rows based on the two boundary elements, then callremove
.It can also be done with vanilla js
Here we loop through the rows till we hit a row with the a row with the id ‘deleteFirstRow’ then we set deleting to true and then we remove() every row until we hit the row id ‘deleteLastRow’ and we remove that row too.
Edit: fix typo