Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete row table using jQuery

Tags:

jquery

Suppose I have a table like this

id  name    address     action
--------------------------------------
s1  n1  a1      delete
s2  n2  a2      delete

Delete is a link for example <a href="http://localhost/student/delete/1">. In the real case I delete the student using ajax. In order to simplify the code, I just alert the link and omit the ajax script. I just wanna know How to delete row from the html document using jquery.

$(document).ready(function() {
$("a").click(function(event) {
    alert("As you can see, the link no longer took you to jquery.com");
        var href = $(this).attr('href');
        alert(href);
        event.preventDefault();
   });
);

I want, After I alert the link the selected row will be remove automatically. Is there any suggestion how to implement this one ?

like image 430
Adi Sembiring Avatar asked Jan 20 '10 10:01

Adi Sembiring


People also ask

How do you delete a row in a table?

Right-click in a table cell, row, or column you want to delete. On the menu, click Delete Cells. To delete one cell, choose Shift cells left or Shift cells up. To delete the row, click Delete entire row.

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

$id); $totalrows = mysqli_num_rows($checkRecord); if($totalrows > 0){ // Delete record $query = "DELETE FROM authors WHERE id=". $id; mysqli_query($con,$query); echo 1; exit; }else{ echo 0; exit; } } echo 0; exit; ?> ✌️ Like this article?

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

The deleteRow() method removes the row at the specified index from a table. Tip: Use the insertRow() to create and insert a new row.


2 Answers

You don't need to call preventDefault(). Simply returning false from the event handler has the same effect.

To remove the row where the <a> link is located, you can call $(this).closest("tr").remove():

$(document).ready(function() {
$("a").click(function(event) {
    alert("As you can see, the link no longer took you to jquery.com");
    var href = $(this).attr('href');
    alert(href);
    $(this).closest("tr").remove(); // remove row
    return false; // prevents default behavior
   });
);
like image 68
Philippe Leybaert Avatar answered Oct 30 '22 16:10

Philippe Leybaert


Add an id to each <tr> called row_9714 and add an id 9714 to the link. Then in your click event handler call:

var thisId = $(this).attr('id');
$("#row_" + thisId).remove();

Note -- 9714 is just a dummy ID. Just use a unique number here for each row.

like image 21
psychotik Avatar answered Oct 30 '22 16:10

psychotik