Search code examples
javascripthtmldomremovechild

Add EventListener Function to Button That Returns Parent ID


For each client in a list the app adds the following li and elements to a ul.
btna has an event listener that I want to remove the full li for that client from the ul.

function clientListEntry(client){
    //HTML Elements
    var clientEntry = document.createElement("LI");

    var diva = document.createElement("div");
    var divb = document.createElement("div");
    var divc = document.createElement("div");
    var divd = document.createElement("div");

    var labela = document.createElement("label");
    var inputa = document.createElement("checkbox");
    var spana = document.createElement("span");
    var btna = document.createElement("button");

    //LI - ID and Class
    clientEntry.id = client.userID;
    clientEntry.classList.add("item_portion");

    //Div classes and username
    diva.classList.add("c_leftsection");
    divb.classList.add("c_block");
    divc.classList.add("c_midsection");
    divc.innerText = client.name;
    divd.classList.add("c_rightsection");

    //Functionals classes
    labela.classList.add("switch");
    inputa.checked = true;
    spana.classList.add("slider");
    spana.classList.add("round");
    btna.classList.add("c_remove");
    btna.innerText = "-";
    btna.addEventListener("click", function() {
        document.removeChild(clientEntry);
    }, false);


    //Add to document
    document.getElementById("tabs_client_list").appendChild(clientEntry);
    clientEntry.appendChild(diva);
    diva.appendChild(divb);
    clientEntry.appendChild(divc);
    clientEntry.appendChild(divd);
    divd.appendChild(labela);
    labela.appendChild(inputa);
    inputa.appendChild(spana);
    divd.appendChild(btna);
}

The ul would look something like this:

            <li id="c2" class="item_portion">
                <div class="c_leftsection">
                    <div class="c_block"></div>
                </div>
                <div class="c_midsection">Jimmy</div>
                <div class="c_rightsection">
                    <label class="switch">
                        <input type="checkbox" checked>
                        <span class="slider round"></span>
                    </label>
                    <button class="c_remove" id="c_btn_remove">-</button>
                </div>
            </li>

Eventually, I will want to remove the client from the list as well as another element of the DOM based off the LI id.
What I think I need is to pass the parent LIs id into a function, but I'm not sure how to retrieve it.


Solution

  • I'm not 100% sure if I understood your question right. Am I right if I think you want to delete the parent LI if you press the button?

    If yes, you could add the ID of the parent-LI to the button like this:

    <button class="c_remove" id="c_btn_remove" data-parent_id="add ID of LI here">-</button>
    

    by clicking the button, the event-listener can access the data-information.

    or you could remove the parent's parent of the button (jQuery example):

    $("#c_btn_remove").click(function(){
        $(this).parent().parent().remove();
    });
    

    maybe closest will work too:

    $("#c_btn_remove").click(function(){
        $(this).closest('li').remove();
    });