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

Submitted by: 
Language: 
Visitors have accessed this post 364 times.

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="<a href="https://sourcecodester.com">Sourcecodster</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodster</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">Create, Read, Update, Delete (CRUD) Using JavaScript Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-8">
  18. <table class="table table-bordered">
  19. <thead class="alert-info">
  20. <tr>
  21. <th>Name</th>
  22. <th>Action</th>
  23. </tr>
  24. </thead>
  25. <tbody id="result"></tbody>
  26. </table>
  27. </div>
  28.  
  29. <div class="col-md-4">
  30. <div id="create">
  31. <form action="javascript:void(0);" method="POST" class="form-group" onsubmit="Create()">
  32. <label>Full Name:</label>
  33. <input type="text" id="name" class="form-control"/>
  34. <br />
  35. <center><button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Create</button></center>
  36. </form>
  37. </div>
  38. <div id="edit">
  39. <form action="javascript:void(0);" method="POST" class="form-group" id="update">
  40. <label>Full Name:</label>
  41. <input type="text" id="update_name" class="form-control"/>
  42. <br />
  43. <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>
  44. </form>
  45. </div>
  46. </div>
  47. </div>
  48. </body>
  49. <script src="js/script.js"></script>
  50. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.