Skip to main content

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. .el{
  7. cursor:pointer;
  8. }
  9. </style>
  10. </head>
  11. <nav class="navbar navbar-default">
  12. <div class="container-fluid">
  13. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  14. </div>
  15. </nav>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6 well">
  18. <h3 class="text-primary">Update Row Using JavasScript</h3>
  19. <hr style="border-top:1px dotted #ccc;"/>
  20. <div class="col-md-8">
  21. <h3>MY MOVIE LIST</h3>
  22. <table class="table table-bordered">
  23. <thead class="alert-info">
  24. <th>Title</th>
  25. </thead>
  26. <tbody id="result"></tbody>
  27. </table>
  28. </div>
  29. <div class="col-md-4">
  30. <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  31. <label>Movie Name:</label>
  32. <input type="text" id="movie_name" class="form-control"/>
  33. <br /><br />
  34. <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>
  35. </form>
  36. </div>
  37. </div>
  38. </body>
  39. <script src="js/script.js"></script>
  40. </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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.