JavaScript - Update HTML List

Submitted by: 
Visitors have accessed this post 170 times.

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

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="<a href="">Sourcecodster</a>
  14. " rel="nofollow">">Sourcecodster</a>
  15. </a> </div>
  16. </nav>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6 well">
  19. <h3 class="text-primary">JavaScript - Update HTML List</h3>
  20. <hr style="border-top:1px dotted #ccc;"/>
  21. <div class="col-md-4">
  22. <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  23. <label>Movie Name:</label>
  24. <input type="text" id="movie_name" class="form-control"/>
  25. <br /><br />
  26. <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>
  27. </form>
  28. </div>
  29. <div class="col-md-8">
  30. <h3>MY MOVIE LIST</h3>
  31. <ul id="result"></ul>
  32. </div>
  33. </div>
  34. </body>
  35. <script src="js/script.js"></script>
  36. </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"];
  3. displayAll();
  4. removeAll();
  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. }
  14. document.getElementById('result').innerHTML = data;
  15. };
  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. }
  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;
  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!

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.