Skip to main content

JavaScript - Update Array Object


In this tutorial we will create a Update Array Object using JavaScript. This code will dynamically update an array value when the user click the edit button. The code use onclick() to call a certain function that can update an array value by applying an array index position as a parameter in the Edit(). 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">JavaScript - Update Array Object</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-4">
  17. <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  18. <label>Full Name:</label>
  19. <input type="text" id="edit_name" class="form-control"/>
  20. <br /><br />
  21. <center><button type="submit" id="btn_update" class="btn btn-warning"><span class="glyphicon glyphicon-update"></span> Update</button> <button type="button" class="btn btn-danger" id="btn_cancel" onclick="Cancel()">Cancel</button></center>
  22. </form>
  23. </div>
  24. <div class="col-md-8">
  25. <table class="table table-bordered">
  26. <thead class="alert-info">
  27. <tr>
  28. <th>Full Name</th>
  29. <th>Action</th>
  30. </tr>
  31. </thead>
  32. <tbody id="result"></tbody>
  33. </table>
  34. </div>
  35. </div>
  36. </body>
  37. <script src="js/script.js"></script>
  38. </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 names = ["John Smith", "Claire Temple"];
  2.  
  3. displayAll();
  4. Cancel();
  5.  
  6. function displayAll() {
  7. var data = '';
  8. if (names.length > 0) {
  9. for (i = 0; i < names.length; i++) {
  10. data += '<tr>';
  11. data += '<td>' + names[i] + '</td>';
  12. data += '<td colspan="2"><center><button class="btn btn-warning" onclick="Edit(' + i + ')"><span class="glyphicon glyphicon-edit"></span> Edit</button></center></td>';
  13. data += '</tr>';
  14. }
  15. }
  16.  
  17. document.getElementById('result').innerHTML = data;
  18. };
  19.  
  20.  
  21. function Edit(item) {
  22. var el = document.getElementById('edit_name');
  23. el.value = names[item];
  24. document.getElementById('edit_name').removeAttribute("disabled");
  25. document.getElementById('btn_update').removeAttribute("disabled");
  26. document.getElementById('btn_cancel').removeAttribute("disabled");
  27. self = this;
  28.  
  29. document.getElementById('update').onsubmit = function() {
  30. var name = el.value;
  31. if (name) {
  32. self.names.splice(item, 1, name.trim());
  33. self.displayAll();
  34. Cancel();
  35. }
  36. }
  37. };
  38.  
  39.  
  40. function Cancel() {
  41. document.getElementById('edit_name').setAttribute("disabled", "disabled");
  42. document.getElementById('edit_name').value="";
  43. document.getElementById('btn_update').setAttribute("disabled", "disabled");
  44. document.getElementById('btn_cancel').setAttribute("disabled", "disabled");
  45. }

There you have it we successfully created a Update Array Object 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.