1

I have to create a list based on the input of the user and then an option to remove an element if it's the case, I tried this.

My idea was to create the list and at the bottom have this "remove button" that once clicked on it it should have added a "remove" option for each element in the list but instead, I end up replacing all the elements of the list with this remove button, any tips?

    
        function addmember(){

            var z = getInputValue();
            var ul= document.getElementById("crew1");
            var li = document.createElement("li");
            li.appendChild(document.createTextNode(z));
            ul.appendChild(li);

        }
        function remove (){
            var r = document.getElementById("crew1");
            var z = r.getElementsByTagName("li");

            for(i = 0; i<z.length; i++){
                z[i].innerHTML ="<button type=\"button\" \">Remove a member</button>"
            }
        }

        function getInputValue(){
            // Selecting the input element and get its value 
            var inputVal = document.getElementById("myInput").value;

            // Displaying the value
            return inputVal;
        }
    
<html lang="en">
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
    <input type="text" placeholder="Type something..." id="myInput">
    <button type="button" onclick="addmember();">Add Member</button>

    <p>Crew:</p>
    <ul class = "prova" id="crew1">

    </ul>
    <button type="button" onclick="remove();">Remove</button>
</body>
</html>
matthias_h
  • 11,356
  • 9
  • 22
  • 40
franco asd
  • 23
  • 3

5 Answers5

1

I have updated your code a little bit, instead of adding listener to each button, you can attach listener to parent and call remove, and check if event target is button and remove it's parent 'li'.

<html lang="en">
    <head>
    <meta charset="utf-8">
    <title>test</title>
    </head>
    <body>
        <input type="text" placeholder="Type something..." id="myInput">
        <button type="button" onclick="addmember();">Add Member</button>
    
        <p>Crew:</p>
    <ul class = "prova" id="crew1" onclick="remove(event)">
    
    </ul>
    
    
    
        <script>
            function addmember(){
    
                var z = getInputValue();
                var ul= document.getElementById("crew1");
                var li = document.createElement("li");
                li.appendChild(document.createTextNode(z));
                var button = document.createElement('button');
       button.innerHTML = 'remove';
       li.appendChild(button);
                ul.appendChild(li);
    
            }
            function remove (event) {
       if(event.target.type == 'submit') {
        event.target.parentElement.remove();
       }
            }
    
            function getInputValue(){
                // Selecting the input element and get its value 
                var inputVal = document.getElementById("myInput").value;
    
                // Displaying the value
                return inputVal;
            }
        </script>
    </body>
    </html>
Hamza Arshad
  • 856
  • 5
  • 8
  • Can u explain me the function: function remove (event) { if(event.target.type == 'submit') { event.target.parentElement.remove(); } } i don't understand what's happening – franco asd Apr 30 '20 at 20:05
  • yes, actually we are adding a single listener to the parent, so now if you will click on
  • text, it will trigger this function too, but we only want to remove when a button is pressed. so here at this line (event.target.type == 'submit') we are checking if a button is pressed, then simply remove it's parent, which is
  • .
  • – Hamza Arshad Apr 30 '20 at 20:07
  • Thank you! That is helpful! – franco asd Apr 30 '20 at 20:15