Skip to main content

JavaScript - Update HTML List


In this tutorial we will create a Update HTML List using JavaScript. This code will update an list value when the user click it and enter the new value. The code use onclick() to call a certain function that can update an list value by adding 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. .el{
  7. cursor:pointer;
  8. }
  9. </style>
  10. </head>
  11. <nav class="navbar navbar-default">
  12. <div class="container-fluid">
  13. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  14. </div>
  15. </nav>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6 well">
  18. <h3 class="text-primary">JavaScript - Update HTML List</h3>
  19. <hr style="border-top:1px dotted #ccc;"/>
  20. <div class="col-md-4">
  21. <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  22. <label>Movie Name:</label>
  23. <input type="text" id="movie_name" class="form-control"/>
  24. <br /><br />
  25. <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>
  26. </form>
  27. </div>
  28. <div class="col-md-8">
  29. <h3>MY MOVIE LIST</h3>
  30. <ul id="result"></ul>
  31. </div>
  32. </div>
  33. </body>
  34. <script src="js/script.js"></script>
  35. </html>

Creating the Script

This code contains the script of the application. This code will update an list 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 = ["Black Widow", "IT Chapter Two", "Frozen 2", "Toy Story 4"];
  2.  
  3. displayAll();
  4. removeAll();
  5.  
  6. function displayAll() {
  7. var data = '';
  8. if (names.length > 0) {
  9. for (i = 0; i < names.length; i++) {
  10. data += '<li>' + names[i] + ' <span class="glyphicon glyphicon-edit text-warning el" onclick="Edit(' + i + ')"></span></li>';
  11. }
  12. }
  13.  
  14. document.getElementById('result').innerHTML = data;
  15. };
  16.  
  17.  
  18. function removeAll() {
  19. document.getElementById('movie_name').setAttribute("disabled", "disabled");
  20. document.getElementById('movie_name').value="";
  21. document.getElementById('btn_update').setAttribute("disabled", "disabled");
  22. document.getElementById('btn_cancel').setAttribute("disabled", "disabled");
  23. }
  24.  
  25. function Edit(item) {
  26. var el = document.getElementById('movie_name');
  27. el.value = names[item];
  28. document.getElementById('movie_name').removeAttribute("disabled");
  29. document.getElementById('btn_update').removeAttribute("disabled");
  30. document.getElementById('btn_cancel').removeAttribute("disabled");
  31. self = this;
  32.  
  33. document.getElementById('update').onsubmit = function() {
  34. var name = el.value;
  35. if (name) {
  36. self.names.splice(item, 1, name.trim());
  37. self.displayAll();
  38. removeAll();
  39. }
  40. }
  41. };

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