Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:03:55+00:00 2026-06-13T16:03:55+00:00

So I’m trying to to basically dynamically create li’s inside an array, and I

  • 0

So I’m trying to to basically dynamically create li’s inside an array, and I would like to create a ‘delete’ button within each li, so that when I click that li, I can delete that specific li.

I know this seems very basic, but I’ve been looking at JS for hours now, and am starting to really confuse myself here.
I keep getting errors like addChild() is not a function… I feel like I’m close, but no cigar. Thanks in advance!

Anyway, here’s my add function:

function add(){
    var deleteBtn = document.createElement('input');
    deleteBtn.type = 'submit';
    deleteBtn.name = 'addButton';
    deleteBtn.className = 'deleteButton';

    for(i=0;i<1;i++){
        id_number[i] = i+1;

        var newSong = '<li class="li_test" id="' + id_number[i] + '">' + "<span>" + "</span>" + '</li>';
        // $(newSong).appendChild(deleteBtn);
        $(deleteBtn).appendTo("#playlist-1");
        $(newSong).appendTo("#playlist-1");
        showList.push(newSong);

        deleteBtn.addEventListener('click', function(evt) {
            deleteFromPlaylist(newSong);
        });
    }
 }

Here’s my delete function

function deleteFromPlaylist(newSong){
    var deleteBtn = document.getElementsByTagName('deleteButton');
    // var deleteMe = deleteBtn.parentNode;
    alert(deleteBtn);
    for(i=0;i<showList.length;i++){
        if(newSong === showList[i]){
            showList.splice(i,1);
            // var pp = p.parentNode;

            // pp.removeChild (p);
            deleteMe = deleteMe.parentNode.remove("li_test");
            deleteMe.removeChild(deleteBtn);
        }
    // console.log(deleteMe);
      }
    }

EDIT: 1 More Related Question

I would like to only add an item if it doesn’t exist already in the array. Here is what I have so far. Any tips on where I’m going wrong?

for (i = 0; i < showList.length; i++) {

    if (newSong !== showList[i]){
        ul_list.innerHTML = newSong;
        container_div.appendChild(ul_list); //append the info
        container_div.appendChild(deleteBtn);

        document.getElementById('playlist-1').appendChild(container_div); //finally add it to the playlist div

        showList.push(newSong);

        deleteBtn.addEventListener('click', function(evt) {
            deleteFromPlaylist(evt, newSong);
        });
        inc++;
        alert("It IS in the Array!");
    }else{
        alert("This already exists!");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:03:57+00:00Added an answer on June 13, 2026 at 4:03 pm

    I’ve altered your code and functions to purely use javascript, instead of a mixture containg jquery. I’ve added comments in the code to explain my actions. If you have any questions, feel free to ask.

    var showList = [];
    var inc = 1;
    
    function add() {
        //create the container element.  If we do this, keeping track of all elements
        //becomes easier, since we just have to remove the container.
        var container_div = document.createElement('div');
        container_div.id = "cont_" + inc;
    
        var ul_list = document.createElement('ul');
    
        var deleteBtn = document.createElement('input');
        deleteBtn.type = 'button';
        deleteBtn.value = 'remove song';
        deleteBtn.name = 'addButton';
        deleteBtn.className = 'deleteButton';
    
        var id_number = [];
        var newSong = "";
        for (i = 0; i < 1; i++) {
            id_number[i] = i + 1;
    
            newSong += '<li class="li_test" id="cont_' + inc + '_song_id_' + id_number[i] + '">' + "<span>test " + inc + "</span>" + '</li>\n'; //all ids must be unique, so we construct it here
        }
    
        ul_list.innerHTML = newSong;
        container_div.appendChild(ul_list); //append the info
        container_div.appendChild(deleteBtn);
    
        document.getElementById('playlist-1').appendChild(container_div); //finally add it to the playlist div
    
        showList.push(newSong);
    
        deleteBtn.addEventListener('click', function(evt) {
            deleteFromPlaylist(evt, newSong);
        });
        inc++;
    }
    
    function deleteFromPlaylist(evt, newSong) {
        var deleteBtn = evt.target; //target the button clicked, instead of a list of all buttons
        var container_div = deleteBtn.parentNode; //get the parent div of the button
        var cont_parent = container_div.parentNode; //and the parent of the container div
        for (i = 0; i < showList.length; i++) {
            if (newSong === showList[i]) {
                showList.splice(i, 1);
            }
        }
        cont_parent.removeChild(container_div); //finally, remove the container from the parent
    }
    

    Update:
    I’ve modified the above function to strictly use objects, rather than strings, because it is easier to extract relevant information from objects, than strings.

    I’ve added in comments to assist with understanding the code. Again, if you have any questions, feel free to ask.

    function add() {
        var list_bool;
        //create the container element.  If we do this, keeping track of all elements
        //becomes easier, since we just have to remove the container.
        var container_div = document.createElement('div');
        container_div.id = "cont_" + inc;
    
        var ul_list = document.createElement('ul');
    
        var deleteBtn = document.createElement('input');
        deleteBtn.type = 'button';
        deleteBtn.value = 'remove song';
        deleteBtn.name = 'addButton';
        deleteBtn.className = 'deleteButton';
    
        var list_item = document.createElement("li"); //create list element
        list_item.className = "li_test"; //set element class
        var list_span = document.createElement("span"); //create span element
        list_span.innerHTML = "test"; //set span text
        list_item.appendChild(list_span); //append span to list element
    
        ul_list.appendChild(list_item); //append list element to un-ordered list element
        var list_bool = false; //create local boolean variable
        if (showList.length > 0) { // loop through showList if it isn't empty
            for (var i = 0; i < showList.length; i++) {
                if (showList[i].innerText !== list_item.innerText) {
                    list_bool = true; //if song exists(comparing text values, set bool to true
                } else if (showList[i].innerText === list_item.innerText) {
                    list_bool = false; //else, set it to false
                    break; //break out of loop.. we don't want it becoming true again, now do we?
                }
            }
        } else {
            list_bool = true; //showList is empty, set to true
        }
        if (list_bool) { //if true, do action of appending to list
            container_div.appendChild(ul_list); //append the info
            container_div.appendChild(deleteBtn);
    
            document.getElementById('playlist-1').appendChild(container_div); //finally add it to the playlist div
            showList.push(list_item);
            deleteBtn.addEventListener('click', function(evt) {
                deleteFromPlaylist(evt, newSong);
            });
            inc++;
        }
    }
    

    DEMO, notice that add() is executed twice, but because the song ‘test’ already exists, it only executes the end action once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.