Javascript - Simple CRUD

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

In this tutorial we will create a Simple CRUD using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding.

Before we started:

This is the link for the bootstrap that has been used for the layout 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 CRUD</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-1"></div>
  18. <div class="col-md-10">
  19. <form action="javascript:void(0);" method="POST" class="form-inline" onsubmit="Add()">
  20. <label>Name:</label>
  21. <input type="text" id="name" class="form-control"/>
  22. <button type="submit" class="btn btn-primary form-control"><span class="glyphicon glyphicon-plus"></span> Add</button>
  23. </form>
  24. <br />
  25. <div id="edit">
  26. <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  27. <label>Name:</label>
  28. <input type="text" id="edit_name" class="form-control"/>
  29. <button type="submit" class="btn btn-warning form-control"><span class="glyphicon glyphicon-update"></span> Update</button> <a class="btn btn-danger" onclick="Close()" aria-label="Close">&#10006;</a>
  30. </form>
  31. </div>
  32. <br />
  33. <table class="table table-bordered">
  34. <thead>
  35. <tr>
  36. <th>Name</th>
  37. <th>Action</th>
  38. </tr>
  39. </thead>
  40. <tbody id="name_list">
  41.  
  42. </tbody>
  43. </table>
  44. </div>
  45. </div>
  46. </body>
  47. <script src="js/script.js"></script>
  48. </html>

Creating the Script

This code contains the script of the application. 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 folder.

  1. var el = document.getElementById('name_list');
  2. var names = [];
  3.  
  4. ListAll();
  5. Close();
  6.  
  7. function ListAll() {
  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 Add() {
  23. var el = document.getElementById('name')
  24. var name = el.value;
  25. if (name) {
  26. names.push(name.trim());
  27. el.value = '';
  28. ListAll();
  29. }
  30. ListAll();
  31. };
  32.  
  33. function Delete(item) {
  34. names.splice(item, 1);
  35. ListAll();
  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 = 'block';
  43. self = this;
  44.  
  45. document.getElementById('update').onsubmit = function() {
  46. var name = el.value;
  47. if (name) {
  48. self.names.splice(item, 1, name.trim());
  49. self.ListAll();
  50. Close();
  51. }
  52. }
  53. };
  54.  
  55.  
  56. function Close() {
  57. document.getElementById('edit').style.display = 'none';
  58. }

There you have it we successfully created a Simple 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.