Add Edit Delete Table Row Example using JQuery

Today, We want to share with you Add Edit Delete Table Row Example using JQuery.In this post we will show you add edit delete in html table with javascript, hear for java-script delete table row onclick we will give you demo and example for implement.In this post, we will learn about add edit and delete rows of a html table with an example.

Add Edit Delete Table Row Example using JQuery

There are the Following The simple About remove table row on button click Full Information With Example and source code.

As I will cover this Post with live Working example to develop how to edit data in html table using javascript, so the some major files and Directory structures for this example is following below.

Read Also:  SQL left outer join Examples

Step 1: Include jQuery library

At first we need to include jQuery Latest library file.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Step 2: JavaScript/jQuery Source Code:

This file All the Data contains the following JavaScript/jQuery Source codes.

$("form").submit(function(e){
        e.preventDefault();
        var name = $("input[name='name']").val();
        var email = $("input[name='email']").val();
     
        $(".data-table tbody").append("<tr data-name='"+name+"' data-email='"+email+"'><td>"+name+"</td><td>"+email+"</td><td><button class='btn btn-info btn-xs edit-data'>Edit</button><button class='btn btn-danger btn-xs btn-delete'>Delete</button></td></tr>");
    
        $("input[name='name']").val('');
        $("input[name='email']").val('');
    });
   
    $("body").on("click", ".btn-delete", function(){
        $(this).parents("tr").remove();
    });
    
    $("body").on("click", ".edit-data", function(){
        var name = $(this).parents("tr").attr('data-name');
        var email = $(this).parents("tr").attr('data-email');
    
        $(this).parents("tr").find("td:eq(0)").html('<input name="edit_name" value="'+name+'">');
        $(this).parents("tr").find("td:eq(1)").html('<input name="edit_email" value="'+email+'">');
    
        $(this).parents("tr").find("td:eq(2)").prepend("<button class='btn btn-info btn-xs update-data'>Update</button><button class='btn btn-warning btn-xs cancel-data'>Cancel</button>")
        $(this).hide();
    });
   
    $("body").on("click", ".cancel-data", function(){
        var name = $(this).parents("tr").attr('data-name');
        var email = $(this).parents("tr").attr('data-email');
    
        $(this).parents("tr").find("td:eq(0)").text(name);
        $(this).parents("tr").find("td:eq(1)").text(email);
   
        $(this).parents("tr").find(".edit-data").show();
        $(this).parents("tr").find(".update-data").remove();
        $(this).parents("tr").find(".cancel-data").remove();
    });
   
    $("body").on("click", ".update-data", function(){
        var name = $(this).parents("tr").find("input[name='edit_name']").val();
        var email = $(this).parents("tr").find("input[name='edit_email']").val();
    
        $(this).parents("tr").find("td:eq(0)").text(name);
        $(this).parents("tr").find("td:eq(1)").text(email);
     
        $(this).parents("tr").attr('data-name', name);
        $(this).parents("tr").attr('data-email', email);
    
        $(this).parents("tr").find(".edit-data").show();
        $(this).parents("tr").find(".cancel-data").remove();
        $(this).parents("tr").find(".update-data").remove();
    });

Step 3: HTML Source Code:

This file contains the following HTML Markup Source codes.

Read Also:  Shell Script to Add Two Numbers using ommand line arguments

<form>
        
    <div class="form-group">
      <label>Name:</label>
      <input type="text" name="name" class="form-control" value="Admin" required="">
    </div>
    
    <div class="form-group">
      <label>Email:</label>
      <input type="text" name="email" class="form-control" value="[email protected]" required="">
    </div>
   
    <button type="submit" class="btn btn-success save-btn">Save</button>
    
  </form>
  <br/>
  <table class="table table-bordered data-table">
    <thead>
      <th>Name</th>
      <th>Email</th>
      <th width="200px">Action</th>
    </thead>
    <tbody>
    
    </tbody>
  </table>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about jquery add edit delete table row example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.