JavaScript Array Add and Remove Demo

This interactive demo lets you add values to an array and remove any displayed value by index. It keeps the original demo purpose but uses DOM methods and event listeners instead of inline JavaScript and generated HTML strings.

Total elements: 0

    Core JavaScript Used in the Demo Top ↑

    const data = [];
    
    data.push(value);        // add at the end
    data.splice(index, 1);  // remove one item at a known index

    The complete live demo above rebuilds the displayed list after each change and uses textContent for user-supplied values, avoiding HTML injection through the input.

    Complete HTML Pattern for the Demo Top ↑

    <label for="arrayItem">Value to add</label>
    <input id="arrayItem" type="text">
    <button id="addItem" type="button">Add to array</button>
    <ul id="arrayItems"></ul>
    
    <script>
    const data = [];
    const input = document.getElementById("arrayItem");
    const list = document.getElementById("arrayItems");
    
    document.getElementById("addItem").addEventListener("click", () => {
      const value = input.value.trim();
      if (value === "") return;
      data.push(value);
    });
    
    // Remove one item when its current index is known
    data.splice(index, 1);
    </script>

    push() Tutorial push() Demo indexOf() splice() Tutorial

    Empty an Array Array Reference Display Array Elements




    Subscribe to our YouTube Channel here



    plus2net.com







    anjel

    07-03-2015

    hi..
    useful code.but i have a doubt what if i want to display particular element of any index and that index value is to be taken by entering values though users.
    smo

    13-03-2015

    You can display the value by reading the key of the array. In side the function you can see one for loop is used with variable i, if i value is taken from user then the corresponding value can be displayed.
    Himari Biswas

    20-01-2016

    thank u very much finally my problem solved
    mr red

    21-02-2017

    how about, if the data in the arrays are on the table? what will the code format is?
    smo1234

    23-02-2017

    You have to first collect the data from the table by using any server side script and SQL.

    While displaying the data you can use the array push to insert data to the JavaScript array. Note that while using server side script you have to echo or print the client side JavaScript code to store values inside array.

    17-04-2021

    Thank you so much. This will help me



    ✖
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
    © 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer