I understand how to get the attribute of an table object $('#tableid tr').attr('data-id')
that uses HTML5 data- attributes. However when I try to remove the row based on the data-id attribute, it doesn't seem to work.
When I did something like this:
var theRowId = $('#tableid tr').attr('data-id');
$('#tableid tr#'+theRowId).remove();
it didn't work. Html 5 data-attributes should be handled like any other attribute, correct?
The jQuery remove() method is used to remove a row from HTML table. jQuery remove() Method: This method removes the selected elements alongwith text and child nodes. This method also removes data and events of the selected elements.
To prevent this, use . removeAttr() alongside . removeData() to remove the data- attribute as well.
Adding a row: Then we will use the jQuery “click” event to detect a click on the add row button and then use the . append() method of jQuery to add a row in the table. Each row element has been assigned an id Ri that we will later use to delete a row. Each element has a row index column and remove the button column.
The jQuery function removes all the rows except the first one using the remove() method. Syntax: $('#btn'). click(function () { $("#rTable").
You need to pass the Index of the tr you want the data-attribute from
$('#tableid tr:eq(0)');
// First row in table
$('#tableid tr:eq(1)');
// Second row in table
Because there might be multiple rows in the table
var theRowId = $('#tableid tr:eq(1)').attr('data-id'); // Get the Second Row id
$('#tableid tr#'+theRowId).remove(); // Remove the row with id
OR if you know the ID of the Row.. simply do this
$('#tableid tr[data-id="'+theRowId+'"]').remove();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With