How to Dynamically Update the HTML list in JavaScript

How to Dynamically Update the HTML list in JavaScript


In this tutorial we will create a How to Dynamically Update the HTML list in JavaScript. This tutorial purpose is to teach you how you can update the html list This will tackle all the important functionality that allow you to update the list. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want to update a list. I will give my best to provide you the easiest way of creating this program Update HTML List. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will display the html form inputs and the HTML list. To create this simply copy and write it into your text editor, then save it 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. <body onload="initFunc();">
  12. <nav class="navbar navbar-default">
  13. <div class="container-fluid">
  14. <a class="navbar-brand" href="">Sourcecodster</a>
  15. </div>
  16. </nav>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6 well">
  19. <h3 class="text-primary">How to Dynamically Update the HTML list</h3>
  20. <hr style="border-top:1px dotted #ccc;"/>
  21. <div class="col-md-8">
  22. <h3>My Favorite Anime</h3>
  23. <ul id="result"></ul>
  24. </div>
  25. <div class="col-md-4">
  26. <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  27. <input type="text" id="anime_title" class="form-control"/>
  28. <br /><br />
  29. <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="removeAll()">Cancel</button></center>
  30. </form>
  31. </div>
  32. </div>
  33. </body>
  34. <script src="script.js"></script>
  35. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will update the current HTML list when you click the list. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var anime_titles = ["Naruto", "Dragon Ball", "Demon Slayer", "One Piece", "Bleach", "Pokemon"];
  5. function initFunc(){
  6. displayAll();
  7. removeAll();
  8. }
  11. function displayAll() {
  12. var data = '';
  13. if (anime_titles.length > 0) {
  14. for (i = 0; i < anime_titles.length; i++) {
  15. data += '<li>' + anime_titles[i] + ' <span class="glyphicon glyphicon-edit text-warning el" onclick="Edit(' + i + ')">Edit</span></li>';
  16. }
  17. }
  19. document.getElementById('result').innerHTML = data;
  20. };
  23. function removeAll() {
  24. document.getElementById('anime_title').style.display="none";
  25. document.getElementById('anime_title').value="";
  26. document.getElementById('btn_update').style.display="none";
  27. document.getElementById('btn_cancel').style.display="none";
  28. }
  30. function Edit(item) {
  31. var el = document.getElementById('anime_title');
  32. el.value = anime_titles[item];
  33. document.getElementById('anime_title').style.display="inline-block";
  34. document.getElementById('btn_update').style.display="inline-block";
  35. document.getElementById('btn_cancel').style.display="inline-block";
  36. self = this;
  38. document.getElementById('update').onsubmit = function() {
  39. var name = el.value;
  40. if (name) {
  41. self.anime_titles.splice(item, 1, name.trim());
  42. self.displayAll();
  43. removeAll();
  44. }
  45. }
  48. };

In the code above we create a method called Edit(), this function will update the current list with the one you submitted from the form. This code is consist of several setting of attributes and updating the HTML list. To update the list we use the built-in JavaScript function called splice(), this function will try to replace the current list base on the index position you set.


The How to Dynamically Update the HTML list in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Dynamically Update the HTML list in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment