JavaScript - Simple Update Array Data

In this tutorial we will create a Simple Update Array Data using JavaScript. This code will automatically update an array value when the user click the update button. The code use onclick() function to launch a function that can update an array value in the list by sending the array index position in the Edit() to apply the changes. 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 be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer.

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">JavaScript - Simple Update Array Data</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-2"></div>
  17.                 <div class="col-md-8">
  18.                         <div id="edit">
  19.                                 <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  20.                                         <label>User:</label>
  21.                                         <input type="text" id="edit_user" class="form-control"/>
  22.                                         <button type="submit" class="btn btn-warning form-control"><span class="glyphicon glyphicon-save"></span> Update</button> <a class="btn btn-danger" onclick="Close()" aria-label="Close">&#10006;</a>
  23.                                 </form>
  24.                         </div>
  25.                         <br />
  26.                         <table class="table table-bordered">
  27.                                 <thead class="alert-info">     
  28.                                         <tr>
  29.                                                 <th>Users</th>
  30.                                                 <th>Action</th>
  31.                                         </tr>
  32.                                 </thead>
  33.                                 <tbody id="user_list"></tbody>
  34.                         </table>
  35.                 </div>
  36.         </div>
  37. </body>
  38. <script src="js/script.js"></script>
  39. </html>

Creating the Script

This code contains the script of the application. This code will update an array value 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('user_list');
  2. var users = ["John Smith", "Claire Temple"];
  3.  
  4. function display() {
  5.         var data = '';
  6.         if (users.length > 0) {
  7.                 for (i = 0; i < users.length; i++) {
  8.                         data += '<tr>';
  9.                         data += '<td>' + users[i] + '</td>';
  10.                         data += '<td colspan="2"><center><button class="btn btn-warning" onclick="Edit(' + i + ')"><span class="glyphicon glyphicon-edit"></span> Edit</button></td>';
  11.                         data += '</tr>';
  12.                 }
  13.         }
  14.         el.innerHTML = data;
  15. };
  16.  
  17. function Edit(item) {
  18.         var el = document.getElementById('edit_user');
  19.         el.value = users[item];
  20.         document.getElementById('edit').style.display = 'block';
  21.         self = this;
  22.        
  23.         document.getElementById('update').onsubmit = function() {
  24.         var user = el.value;
  25.                 if (user) {
  26.                         self.users.splice(item, 1, user.trim());
  27.                         self.display();
  28.                         Close();
  29.                 }
  30.         }
  31. };
  32.  
  33. function Close() {
  34.         document.getElementById('edit').style.display = 'none';
  35. }
  36.  
  37. Close();       
  38. display();
There you have it we successfully created a Simple Update Array Data 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