How To Delete Multiple Selected Rows Using jQuery

in this jQuery tutorial i will let you know how to delete multiple record from table using jQury with Ajax, Here also we have option to select all or uncheck all record of table. This is very common functionality of any HTML table listing and no one to want reload page after delete record, Now below given methods are used for delete selected rows from grid with help of Ajax.

Checkout other tutorial of jQuery,

delete_mutiple_using_jquery

This jQuery Example have following Functionality

  • Check all & Uncheck all checkboxes using jQuery
  • Delete single row from a table using PHP(Server-side) OR jQuery(Client-side)
  • Delete multiple row from a table with Empty validation using PHP(Server-side) OR jQuery(Client-side)

Simple jQuery Script to Delete Single/Multiple Selected Rows

Step 1: We have included jquery and bootstrap files.

Select Code
1
2
3
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

Step 2: We will define HTML table rows.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<table id="employee_grid" class="table table-condensed table-hover table-striped bootgrid-table" width="60%" cellspacing="0">
   <thead>
      <tr>
         <th><input type="checkbox" id="master"></th>
         <th>Name</th>
         <th>Salary</th>
         <th>Age</th>
         <th class="pull-right">Delete</th>
      </tr>
   </thead>
   <tbody>
      <tr data-row-id="1">
         <td><input type="checkbox" class="sub_chk" data-id="1"></td>
         <td>Tiger Nixon</td>
         <td>320800</td>
         <td>61</td>
         <td><a class="remove-row pull-right" targetDiv="" data-id="1" href="javascript: void(0)"><i class="glyphicon glyphicon-trash"></i></a></td>
      </tr>
      <tr data-row-id="2">
         <td><input type="checkbox" class="sub_chk" data-id="2"></td>
         <td>Garrett Winters</td>
         <td>170750</td>
         <td>63</td>
         <td><a class="remove-row pull-right" targetDiv="" data-id="3" href="javascript: void(0)"><i class="glyphicon glyphicon-trash"></i></a></td>
      </tr>
     .....
     .....
   </tbody>
</table>

Step 3: This function is used for select all record of table.You can use this function to Check all & Uncheck all elements.

Select Code
1
2
3
4
5
6
7
8
9
10
jQuery('#master').on('click', function(e) {
    if($(this).is(':checked',true))  
    {
        $(".sub_chk").prop('checked', true);  
    }  
    else  
    {  
        $(".sub_chk").prop('checked',false);  
    }  
});

Step 4 :Below function is use to delete all records using Ajax request(Server-Side) or jQuery(Client-Side).

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
jQuery('.delete_all').on('click', function(e) { 
var allVals = [];  
        $(".sub_chk:checked").each(function() {  
            allVals.push($(this).attr('data-id'));
        });  
        //alert(allVals.length); return false;  
        if(allVals.length <=0)  
        {  
            alert("Please select row.");  
        }  
        else {  
            //$("#loading").show(); 
            WRN_PROFILE_DELETE = "Are you sure you want to delete this row?";  
            var check = confirm(WRN_PROFILE_DELETE);  
            if(check == true){  
                //for server side
                /*
                var join_selected_values = allVals.join(","); 
                
                $.ajax({   
                  
                    type: "POST",  
                    url: "delete.php",  
                    cache:false,  
                    data: 'ids='+join_selected_values,  
                    success: function(response)  
                    {   
                        $("#loading").hide();  
                        $("#msgdiv").html(response);
                        //referesh table
                    }   
                });*/
              //for client side
              $.each(allVals, function( index, value ) {
                  $('table tr').filter("[data-row-id='" + value + "']").remove();
              });
                

            }  
        }  
    });

JavaScript Function to Delete Single Record

Step 5: Below jQuery script is used to delete single records from HTML table.

Select Code
1
2
3
4
5
6
7
jQuery('.remove-row').on('click', function(e) {
        WRN_PROFILE_DELETE = "Are you sure you want to delete this row?";  
            var check = confirm(WRN_PROFILE_DELETE);  
            if(check == true){
                $('table tr').filter("[data-row-id='" + $(this).attr('data-id') + "']").remove();
            }
    });

Demo & Download Source Code

Please feel free to send queries to me using below comment section.