Menu Close

How to delete a row from a table using jQuery?

How to delete a row from a table using jQuery?

Use event delegation to include buttons for both add a new and delete a table row on a web page using jQuery.

Firstly, set the delete button:

<button type="button" class="deletebtn" title="Remove row">X</button>

To fire event on click of a button, use jQuery on() method:

$(document).on('click', 'button.deletebtn', function () {
   $(this).closest('tr').remove();
   return false;
});

The following is the complete code delete a row from a table using jQuery:

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   <script>
   $(document).ready(function(){
      var x = 1;
      $("#newbtn").click(function () {
         $("table tr:first").clone().find("input").each(function () {
            $(this).val('').attr({
               'id': function (_, id) {
                     return id + x
               },
               'name': function (_, name) {
                  return name + x
               },
               'value': ''
         });
      }).end().appendTo("table");
      x++;
   });

   $(document).on('click', 'button.deletebtn', function () {
      $(this).closest('tr').remove();
      return false;
   });
});
</script>
</head>
<body>
<table>
<tr>
<td>
<button type="button" class="deletebtn" title="Remove row">X</button>
</td>
<td>
<input type="text" id="txtTitle" name="txtTitle">
</td>
<td>
<input type="text" id="txtLink" name="txtLink">
</td>
</tr>
</table>
<button id="newbtn">Add new Row</button>
</body>
</html>
Posted in JavaScript, JQuery