JavaScript - Update Array Object

Submitted by: 
Visitors have accessed this post 136 times.

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

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="">Sourcecodster</a>
  10. " rel="nofollow">">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 - Update Array Object</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  19. <label>Full Name:</label>
  20. <input type="text" id="edit_name" class="form-control"/>
  21. <br /><br />
  22. <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>
  23. </form>
  24. </div>
  25. <div class="col-md-8">
  26. <table class="table table-bordered">
  27. <thead class="alert-info">
  28. <tr>
  29. <th>Full Name</th>
  30. <th>Action</th>
  31. </tr>
  32. </thead>
  33. <tbody id="result"></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 names = ["John Smith", "Claire Temple"];
  3. displayAll();
  4. Cancel();
  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. }
  17. document.getElementById('result').innerHTML = data;
  18. };
  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;
  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. };
  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!

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.