Simple Edit Table Data Using JavaScript

This code will show you how to create Simple Edit Table Data using JavaScript. The program will update your current table data dynamically. The logic of this code is that you need to bind the element value into a button, then fetch it into the input value. To learn more about this, just follow the steps below.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">Simple Edit Table Data Using JavaScript</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-8">
  17.                         <table class="table table-bordered">
  18.                                 <thead class="alert-info">     
  19.                                         <tr>
  20.                                                 <th>Full Name</th>
  21.                                                 <th>Action</th>
  22.                                         </tr>
  23.                                 </thead>
  24.                                 <tbody id="result"></tbody>
  25.                         </table>
  26.                 </div>
  27.                 <div class="col-md-4">
  28.                         <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  29.                                 <label>Full Name:</label>
  30.                                 <input type="text" id="edit_name" class="form-control"/>
  31.                                 <br /><br />   
  32.                                 <center><button type="submit" id="btn_update" class="btn btn-warning"><span class="glyphicon glyphicon-update"></span> Update</button> <button type="button" class="btn btn-danger" id="btn_cancel" onclick="Cancel()">Cancel</button></center>
  33.                         </form>
  34.                 </div>
  35.         </div>
  36. </body>
  37. <script src="js/script.js"></script>
  38. </html>

Creating the Script

This code contains the script of the application. The code will update the table data when user click the edit button. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var names = ["John Smith", "Claire Temple"];
  2.  
  3. displayList();
  4. Cancel();
  5.  
  6. function displayList() {
  7.         var data = '';
  8.         if (names.length > 0) {
  9.                 for (i = 0; i < names.length; i++) {
  10.                         data += '<tr>';
  11.                         data += '<td>' + names[i] + '</td>';
  12.                         data += '<td colspan="2"><center><button class="btn btn-warning" onclick="Edit(' + i + ')"><span class="glyphicon glyphicon-edit"></span> Edit</button></center></td>';
  13.                         data += '</tr>';
  14.                 }
  15.         }
  16.        
  17.         document.getElementById('result').innerHTML = data;
  18. };
  19.  
  20. function Cancel() {
  21.         document.getElementById('edit_name').setAttribute("disabled", "disabled");
  22.         document.getElementById('edit_name').value="";
  23.         document.getElementById('btn_update').setAttribute("disabled", "disabled");
  24.         document.getElementById('btn_cancel').setAttribute("disabled", "disabled");
  25. }
  26.        
  27.  
  28. function Edit(item) {
  29.         var el = document.getElementById('edit_name');
  30.         el.value = names[item];
  31.         document.getElementById('edit_name').removeAttribute("disabled");
  32.         document.getElementById('btn_update').removeAttribute("disabled");
  33.         document.getElementById('btn_cancel').removeAttribute("disabled");
  34.         self = this;
  35.        
  36.         document.getElementById('update').onsubmit = function() {
  37.         var name = el.value;
  38.                 if (name) {
  39.                         self.names.splice(item, 1, name.trim());
  40.                         self.displayList();
  41.                         Cancel();
  42.                 }
  43.         }
  44. };
There you have it we successfully created a Simple Edit Table Data using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Tags

Add new comment