5

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 ?

3
  • 1
    Can you post the HTML markup? Commented Jan 20, 2010 at 10:47
  • Do you want to remove the row from the Database table, or the HTML table? Or both? Commented Jan 20, 2010 at 10:53
  • @Douwe Would you know how to have this delete the html and database table? I just came across this question when searching for an answer for the same sort of problem. Commented Apr 12, 2011 at 16:02

3 Answers 3

18

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
   });
);
Sign up to request clarification or add additional context in comments.

1 Comment

nice... i didn't know about closest
1

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.

4 Comments

according to you, which one is the best practice. I use dummy id like $("#row_" + thisId).remove(); or I use closest selector like $(this).closest("tr").remove(); // remove row
While this will work, I think this solution is a little overkill. It can be done a lot cleaner
why you said that the psychotik answer is overkill ?
Getting the id first, and then building a string to select the row with a specific id is not very efficient. After all, jQuery offers very nice ways of selecting the parent container(s) of any element
0

This example removes rows from a table with JQuery.

$(document).ready(function () {
$("#my_table .remove_row").click(function () {
	$(this).parents("tr:first")[0].remove();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table"  border="1" id="my_table">
   <tbody>
      <tr>
         <td>1</td>
         <td>1</td>
         <td>1</td>
         <td><button type="button" class="remove_row">X</button></td>
      </tr>
      <tr>
         <td>2</td>
         <td>2</td>
         <td>2</td>
         <td><button type="button" class="remove_row">X</button></td>
      </tr>
   </tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.