How to Update HTML list Dynamically using Javascript

0 885

The process of Update HTML list Dynamically using Javascript involves modifying the content of the list dynamically based on the provided data. Here is a description of the steps involved:

HTML Structure: To begin, you need to set up the HTML structure where the list will be displayed. In this case, an ordered list <ol> element with a specific ID is used. This ID will be used to target the list in JavaScript for manipulation.

Set up the HTML structure:

<!DOCTYPE html>
<html lang="en">
<title>How to Update  HTML list Dynamically using Javascript</title>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
        margin-top: 100px;
        background: white;
        margin-top: 58px;
        padding: 15px;
<body onload="initFunc();">

<div class="col-md-3"></div>
<div class="col-md-6 well content">
<h3 class="text-success text-center">How to Update  HTML list Dynamically using Javascript</h3>
<hr style="border-top:1px dotted #ccc;"/>
<div class="col-md-6">
    <h3>My Favorite Movies</h3>
    <ol id="result"></ol>
<div class="col-md-6">
    <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
        <input type="text" id="movie_title" class="form-control"/>
        <br /><br />	
        <button type="submit" id="btn_update" class="btn btn-success"><span class="glyphicon glyphicon-update"></span> Update</button> 
        <button type="button" class="btn btn-danger" id="btn_cancel" onclick="removeAll()">Cancel</button>
<script src="script.js"></script>

JavaScript Code: JavaScript is used to update the list dynamically. Firstly, you retrieve the <ol> element using its ID and store it in a variable. Then, you clear the existing list by setting its innerHTML property to an empty string.

Write JavaScript code to update the list dynamically:

var movie_titles = ["Iron Man", "Superman", "Spiderman", "The Godfather", "Seven Samurai", "Chinatown"];

function initFunc(){

function displayAll() {
    var data = '';
    if (movie_titles.length > 0) {
        for (i = 0; i < movie_titles.length; i++) {
            data += '<li>' + movie_titles[i] + ' <span class="glyphicon glyphicon-edit text-danger el" onclick="Edit(' + i + ')"></span></li>';
    document.getElementById('result').innerHTML = data;

function removeAll() {

function Edit(item) {
    var el = document.getElementById('movie_title');
    el.value = movie_titles[item];
    self = this;
    document.getElementById('update').onsubmit = function() {
    var name = el.value;
        if (name) {
            self.movie_titles.splice(item, 1, name.trim());

In the provided JavaScript code, the list items are stored in an array called movie_titles . The code dynamically creates <li> elements for each item in the array and appends them to the <ol> element with the ID result.

Finally, by calling the Edit() function, the list will be populated dynamically with the items in the array. You can modify the items array to include your desired list items.


How to Update HTML list Dynamically using Javascript

Download Source Code

Leave A Reply

Your email address will not be published.