I am trying to remove a tr from DOM after ajax success where td of this tr has a certain data-value value
This is the html:
<tr>
<td data-value="1">First</td>
</tr>
<tr>
<td data-value="2">Second</td>
</tr>
<tr>
<td data-value="3">Third</td>
</tr>
......
My ajax javascript:
success: function (data) {
var identity = data.val; // gives me the number 2
// find td which has data-value of 2 and remove the corresponding tr of it from DOM
$('td[data-value=identity]').closest('tr').remove(); // does not work
},
How can i achieve this?
I would recommend you to be very specific about the table in which it has that 'td' tag.
The above code which was answered in the comment will work well for you only:
It's always a nice practice to specify table name in your jQuery selector & traverse it to particular child element as per requirements, if you want to target any tag inside of a table (as there can be multiple table on a same page in future developments).
You can improvise above code as,
This code will first find the table name inside of entire DOM then traverse to 'tbody' tag of that table & search for 'td' element which has data-value="identity" -> then remove the closest 'tr'.
NOTE: If you want to remove multiple data-value="identity", use .each/.map method to iterate over table for desired results.