Create, Read, Update, Delete (CRUD) Using JavaScript Source Code

In this tutorial we will create a Create, Read, Update, Delete (CRUD) using JavaScript. This code will add, delete, update and read a data table when the user open the program. The code use onclick() function to call a specific method that utilize to a different functionalities in order to manipulate the array as a temporary database in the table. You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

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">Create, Read, Update, Delete (CRUD) Using JavaScript Source Code</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>Name</th>
  21. <th>Action</th>
  22. </tr>
  23. </thead>
  24. <tbody id="result"></tbody>
  25. </table>
  26. </div>
  27.  
  28. <div class="col-md-4">
  29. <div id="create">
  30. <form action="javascript:void(0);" method="POST" class="form-group" onsubmit="Create()">
  31. <label>Full Name:</label>
  32. <input type="text" id="name" class="form-control"/>
  33. <br />
  34. <center><button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Create</button></center>
  35. </form>
  36. </div>
  37. <div id="edit">
  38. <form action="javascript:void(0);" method="POST" class="form-group" id="update">
  39. <label>Full Name:</label>
  40. <input type="text" id="update_name" class="form-control"/>
  41. <br />
  42. <center><button type="submit" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Update</button> <button class="btn btn-danger" onclick="editHider()">&#10006; Close</button></center>
  43. </form>
  44. </div>
  45. </div>
  46. </div>
  47. </body>
  48. <script src="js/script.js"></script>
  49. </html>

Creating the Script

This code contains the script of the application. This code will use CRUD functionalities toan array when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. var el = document.getElementById('result');
  2. var names = [];
  3.  
  4. function Create() {
  5. var el = document.getElementById('name')
  6. var name = el.value;
  7. if (name) {
  8. names.push(name.trim());
  9. el.value = '';
  10. displayData();
  11. }
  12. displayData();
  13. };
  14.  
  15. function Delete(item) {
  16. names.splice(item, 1);
  17. displayData();
  18.  
  19. };
  20.  
  21. function Edit(item) {
  22. var el = document.getElementById('update_name');
  23. el.value = names[item];
  24. document.getElementById('edit').style.display = 'inline';
  25. document.getElementById('create').style.display = 'none';
  26. self = this;
  27.  
  28. document.getElementById('update').onsubmit = function() {
  29. var name = el.value;
  30. if (name) {
  31. self.names.splice(item, 1, name.trim());
  32. self.displayData();
  33. buttonToggle();
  34. document.getElementById('create').style.display = 'inline';
  35. }
  36. }
  37. };
  38.  
  39.  
  40. function buttonToggle() {
  41. document.getElementById('edit').style.display = 'none';
  42. document.getElementById('create').style.display = 'inline';
  43. }
  44.  
  45.  
  46. function displayData() {
  47. var data = '';
  48. if (names.length > 0) {
  49. for (i = 0; i < names.length; i++) {
  50. data += '<tr>';
  51. data += '<td>' + names[i] + '</td>';
  52. data += '<td colspan="2"><center><button class="btn btn-warning" onclick="Edit(' + i + ')"><span class="glyphicon glyphicon-edit"></span> Edit</button> | <button class="btn btn-danger" onclick="Delete(' + i + ')"><span class="glyphicon glyphicon-trash"></span> Delete</button></center></td>';
  53. data += '</tr>';
  54. }
  55. }
  56.  
  57. el.innerHTML = data;
  58. };
  59.  
  60.  
  61. displayData();
  62. buttonToggle();
There you have it we successfully created a Create, Read, Update, Delete (CRUD) 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