Update Row Using JavasScript

In this tutorial we will try to do Update Row using JavaScript. The program will dynamically update the table row by clicking the edit button. This is a free code, you can modify and use this code as your reference. 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.                 <style>
  7.                         .el{
  8.                                 cursor:pointer;
  9.                         }
  10.                 </style>
  11.         </head>
  12.         <nav class="navbar navbar-default">
  13.                 <div class="container-fluid">
  14.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  15.                 </div>
  16.         </nav>
  17.         <div class="col-md-3"></div>
  18.         <div class="col-md-6 well">
  19.                 <h3 class="text-primary">Update Row Using JavasScript</h3>
  20.                 <hr style="border-top:1px dotted #ccc;"/>
  21.                 <div class="col-md-8">
  22.                         <h3>MY MOVIE LIST</h3>
  23.                         <table class="table table-bordered">
  24.                                 <thead class="alert-info">
  25.                                         <th>Title</th>
  26.                                 </thead>
  27.                                 <tbody id="result"></tbody>
  28.                         </table>
  29.                 </div>
  30.                 <div class="col-md-4">
  31.                         <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  32.                                 <label>Movie Name:</label>
  33.                                 <input type="text" id="movie_name" class="form-control"/>
  34.                                 <br /><br />   
  35.                                 <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>
  36.                         </form>
  37.                 </div>
  38.         </div>
  39. </body>
  40. <script src="js/script.js"></script>
  41. </html>

Creating the Script

This code contains the script of the application. The code can modify the entry of table row via form input. 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 = ["Free Guy", "Ip Man", "Karate Kid", "Robocop", "Angry Dogs"];
  2.  
  3. displayAll();
  4. removeAll();
  5.  
  6.  
  7.  
  8.  
  9. function removeAll() {
  10.         document.getElementById('movie_name').setAttribute("disabled", "disabled");
  11.         document.getElementById('movie_name').value="";
  12.         document.getElementById('btn_update').setAttribute("disabled", "disabled");
  13.         document.getElementById('btn_cancel').setAttribute("disabled", "disabled");
  14. }
  15.  
  16. function Edit(item) {
  17.         var el = document.getElementById('movie_name');
  18.         el.value = names[item];
  19.         document.getElementById('movie_name').removeAttribute("disabled");
  20.         document.getElementById('btn_update').removeAttribute("disabled");
  21.         document.getElementById('btn_cancel').removeAttribute("disabled");
  22.         self = this;
  23.        
  24.         document.getElementById('update').onsubmit = function() {
  25.         var name = el.value;
  26.                 if (name) {
  27.                         self.names.splice(item, 1, name.trim());
  28.                         self.displayAll();
  29.                         removeAll();
  30.                 }
  31.         }
  32. };
  33.  
  34. function displayAll() {
  35.         var data = '';
  36.         if (names.length > 0) {
  37.                 for (i = 0; i < names.length; i++) {
  38.                         data += '<tr><td>' + names[i] + ' <span class="glyphicon glyphicon-edit text-warning el" onclick="Edit(' + i + ')"></span></tr></td>';
  39.                 }
  40.         }
  41.        
  42.         document.getElementById('result').innerHTML = data;
  43. };
There you have it we successfully created a Update Row 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!

Add new comment