JavaScript - Simple Create, Read, Update, Delete Function

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

In this tutorial we will create a Simple Create, Read, Update, Delete Function using JavaScript. This code can add, delete, update and read a data when the user open the application. The code use onclick() function to call a specific method that utilize a different functionalities in order to manipulate the array as a temporary database provider. 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">JavaScript - Simple Create, Read, Update, Delete Function</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <div id="create">
  19. <form action="javascript:void(0);" method="POST" class="form-group" onsubmit="Create()">
  20. <label>Full Name:</label>
  21. <input type="text" id="name" class="form-control"/>
  22. <br />
  23. <center><button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Create</button></center>
  24. </form>
  25. </div>
  26. <div id="edit">
  27. <form action="javascript:void(0);" method="POST" class="form-group" id="update">
  28. <label>Full Name:</label>
  29. <input type="text" id="edit_name" class="form-control"/>
  30. <br />
  31. <center><button type="submit" class="btn btn-warning"><span class="glyphicon glyphicon-update"></span> Update</button> <button class="btn btn-danger" onclick="editHider()">&#10006; Close</button></center>
  32. </form>
  33. </div>
  34. </div>
  35.  
  36. <div class="col-md-8">
  37. <table class="table table-bordered">
  38. <thead class="alert-info">
  39. <tr>
  40. <th>Name</th>
  41. <th>Action</th>
  42. </tr>
  43. </thead>
  44. <tbody id="member_list"></tbody>
  45. </table>
  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 manipulate an 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('member_list');
  2. var names = [];
  3.  
  4. displayAll();
  5. editHider();
  6.  
  7. function displayAll() {
  8. var data = '';
  9. if (names.length > 0) {
  10. for (i = 0; i < names.length; i++) {
  11. data += '<tr>';
  12. data += '<td>' + names[i] + '</td>';
  13. 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>';
  14. data += '</tr>';
  15. }
  16. }
  17.  
  18. el.innerHTML = data;
  19. };
  20.  
  21.  
  22. function Create() {
  23. var el = document.getElementById('name')
  24. var name = el.value;
  25. if (name) {
  26. names.push(name.trim());
  27. el.value = '';
  28. displayAll();
  29. }
  30. displayAll();
  31. };
  32.  
  33. function Delete(item) {
  34. names.splice(item, 1);
  35. displayAll();
  36.  
  37. };
  38.  
  39. function Edit(item) {
  40. var el = document.getElementById('edit_name');
  41. el.value = names[item];
  42. document.getElementById('edit').style.display = 'inline';
  43. document.getElementById('create').style.display = 'none';
  44. self = this;
  45.  
  46. document.getElementById('update').onsubmit = function() {
  47. var name = el.value;
  48. if (name) {
  49. self.names.splice(item, 1, name.trim());
  50. self.displayAll();
  51. editHider();
  52. document.getElementById('create').style.display = 'inline';
  53. }
  54. }
  55. };
  56.  
  57.  
  58. function editHider() {
  59. document.getElementById('edit').style.display = 'none';
  60. document.getElementById('create').style.display = 'inline';
  61. }
  62.  
  63.  

There you have it we successfully created a Simple Create, Read, Update, Delete Function 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.