Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove html 5 table row containing data- attribute with jquery

Tags:

html

jquery

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?

like image 752
Kixoka Avatar asked Nov 02 '12 18:11

Kixoka


People also ask

How do you delete a row in a table using jQuery?

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.

How remove data attribute value in jQuery?

To prevent this, use . removeAttr() alongside . removeData() to remove the data- attribute as well.

How do you add and remove rows from a table in HTML using jQuery?

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.

How we can delete all table rows except first one using jQuery?

The jQuery function removes all the rows except the first one using the remove() method. Syntax: $('#btn'). click(function () { $("#rTable").


1 Answers

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();
like image 194
Sushanth -- Avatar answered Sep 27 '22 23:09

Sushanth --