How to delete an from list with javascript [duplicate]










0















This question already has an answer here:



  • JavaScript DOM remove element

    4 answers



What im trying to do is to remove whatever list item a user clicks on from the DOM using javscript. This is my code:



// Delete shape from ul event
shapeList.onclick = function (event)
var shapesArray = shapesCtrl.getShapesArray();
var target = event.target; // Getting which <li> was clicked
var id = target.parentNode.id; // Getting the value of the li that was clicked
canvasCtrl.deleteShape(shapesArray, id); // Deleting the targeted element from the array

var li = shapeList.childNodes;

// Above i remove the object that the corresponds with the li the user clicked on but i cant remove the actual li itself... Ive tried to use li[id].remove() but it just keeps removing the 1st li even though the id might point to the last item for example.

;


Can anybody please help me do this with vanilla js and not jquery.
Thanks! :)










share|improve this question













marked as duplicate by vsync javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 8:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • This is primarily a question about removal of DOM nodes, which has plenty information on Google. The other part of this question is about onClick event listener, which also has more than enough answers on Google. There was no need to ask this question here, Stackoverflow is packed with information on these subjects.
    – vsync
    Nov 13 '18 at 8:04















0















This question already has an answer here:



  • JavaScript DOM remove element

    4 answers



What im trying to do is to remove whatever list item a user clicks on from the DOM using javscript. This is my code:



// Delete shape from ul event
shapeList.onclick = function (event)
var shapesArray = shapesCtrl.getShapesArray();
var target = event.target; // Getting which <li> was clicked
var id = target.parentNode.id; // Getting the value of the li that was clicked
canvasCtrl.deleteShape(shapesArray, id); // Deleting the targeted element from the array

var li = shapeList.childNodes;

// Above i remove the object that the corresponds with the li the user clicked on but i cant remove the actual li itself... Ive tried to use li[id].remove() but it just keeps removing the 1st li even though the id might point to the last item for example.

;


Can anybody please help me do this with vanilla js and not jquery.
Thanks! :)










share|improve this question













marked as duplicate by vsync javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 8:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • This is primarily a question about removal of DOM nodes, which has plenty information on Google. The other part of this question is about onClick event listener, which also has more than enough answers on Google. There was no need to ask this question here, Stackoverflow is packed with information on these subjects.
    – vsync
    Nov 13 '18 at 8:04













0












0








0








This question already has an answer here:



  • JavaScript DOM remove element

    4 answers



What im trying to do is to remove whatever list item a user clicks on from the DOM using javscript. This is my code:



// Delete shape from ul event
shapeList.onclick = function (event)
var shapesArray = shapesCtrl.getShapesArray();
var target = event.target; // Getting which <li> was clicked
var id = target.parentNode.id; // Getting the value of the li that was clicked
canvasCtrl.deleteShape(shapesArray, id); // Deleting the targeted element from the array

var li = shapeList.childNodes;

// Above i remove the object that the corresponds with the li the user clicked on but i cant remove the actual li itself... Ive tried to use li[id].remove() but it just keeps removing the 1st li even though the id might point to the last item for example.

;


Can anybody please help me do this with vanilla js and not jquery.
Thanks! :)










share|improve this question














This question already has an answer here:



  • JavaScript DOM remove element

    4 answers



What im trying to do is to remove whatever list item a user clicks on from the DOM using javscript. This is my code:



// Delete shape from ul event
shapeList.onclick = function (event)
var shapesArray = shapesCtrl.getShapesArray();
var target = event.target; // Getting which <li> was clicked
var id = target.parentNode.id; // Getting the value of the li that was clicked
canvasCtrl.deleteShape(shapesArray, id); // Deleting the targeted element from the array

var li = shapeList.childNodes;

// Above i remove the object that the corresponds with the li the user clicked on but i cant remove the actual li itself... Ive tried to use li[id].remove() but it just keeps removing the 1st li even though the id might point to the last item for example.

;


Can anybody please help me do this with vanilla js and not jquery.
Thanks! :)





This question already has an answer here:



  • JavaScript DOM remove element

    4 answers







javascript html dom html-lists dom-manipulation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 19:13









Jared

52




52




marked as duplicate by vsync javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 8:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by vsync javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 8:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • This is primarily a question about removal of DOM nodes, which has plenty information on Google. The other part of this question is about onClick event listener, which also has more than enough answers on Google. There was no need to ask this question here, Stackoverflow is packed with information on these subjects.
    – vsync
    Nov 13 '18 at 8:04
















  • This is primarily a question about removal of DOM nodes, which has plenty information on Google. The other part of this question is about onClick event listener, which also has more than enough answers on Google. There was no need to ask this question here, Stackoverflow is packed with information on these subjects.
    – vsync
    Nov 13 '18 at 8:04















This is primarily a question about removal of DOM nodes, which has plenty information on Google. The other part of this question is about onClick event listener, which also has more than enough answers on Google. There was no need to ask this question here, Stackoverflow is packed with information on these subjects.
– vsync
Nov 13 '18 at 8:04




This is primarily a question about removal of DOM nodes, which has plenty information on Google. The other part of this question is about onClick event listener, which also has more than enough answers on Google. There was no need to ask this question here, Stackoverflow is packed with information on these subjects.
– vsync
Nov 13 '18 at 8:04












2 Answers
2






active

oldest

votes


















0














Based on your markup:



<ul id ="shape-list" class="list-group mt-3 mb-3"> 
<li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li>
</ul>


You can simply use the Node method removeChild. This is assuming that the i tag is the target in your event.



 target.parentNode.parentNode.removeChild(target.parentNode);


And inside of your code:



// Delete shape from ul event
shapeList.onclick = function (event)
var shapesArray = shapesCtrl.getShapesArray();
var target = event.target; // Getting which <li> was clicked
var id = target.parentNode.id; // Getting the value of the li that was clicked
canvasCtrl.deleteShape(shapesArray, id); // Deleting the targeted element from the array

var li = shapeList.childNodes;

target.parentNode.parentNode.removeChild(target.parentNode);
;





share|improve this answer






















  • Thanks for the quick reply! I forgot to mention the target element is actualy an icon so when i use your code it just delete the little bin. I tried target.parentNode.removeChild(target.parentNode); but it didnt work so im not really sure how to target it
    – Jared
    Nov 12 '18 at 19:25











  • @Jared What does your list look like markup wise?
    – zfrisch
    Nov 12 '18 at 19:29










  • <ul id ="shape-list" class="list-group mt-3 mb-3"> <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li> </ul>
    – Jared
    Nov 12 '18 at 19:33










  • @Jared Updated!
    – zfrisch
    Nov 12 '18 at 19:37






  • 1




    I managed to get it sorted. The problem was i was targeting the li and not the ul. I changed that last line to shapeList.removeChild(target.parentNode) and its working how i want it :) Thanks fro the help though!
    – Jared
    Nov 12 '18 at 19:39


















2














I don't know what your list looks like, but you should be able to adopt this accordingly.






const lis = [...document.querySelectorAll('.this-list li')];

for (const li of lis)
li.addEventListener('click', function()
this.parentNode.removeChild(this);
)

<ul class="this-list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>





If you prefer a delegate listener on the ul itself, here you go:






const ul = document.querySelector('.this-list');

ul.addEventListener('click', function(e)
this.removeChild(e.target);
)

<ul class="this-list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>





This has the advantage that it works for dynamically created list items, too:






const ul = document.querySelector('.this-list');

for (let i=0; i<5; i++)
let li = document.createElement('li');
li.textContent = `Item $i+1`;
ul.appendChild(li);


ul.addEventListener('click', function(e)
this.removeChild(e.target);
)

<ul class="this-list"></ul>








share|improve this answer





























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Based on your markup:



    <ul id ="shape-list" class="list-group mt-3 mb-3"> 
    <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li>
    </ul>


    You can simply use the Node method removeChild. This is assuming that the i tag is the target in your event.



     target.parentNode.parentNode.removeChild(target.parentNode);


    And inside of your code:



    // Delete shape from ul event
    shapeList.onclick = function (event)
    var shapesArray = shapesCtrl.getShapesArray();
    var target = event.target; // Getting which <li> was clicked
    var id = target.parentNode.id; // Getting the value of the li that was clicked
    canvasCtrl.deleteShape(shapesArray, id); // Deleting the targeted element from the array

    var li = shapeList.childNodes;

    target.parentNode.parentNode.removeChild(target.parentNode);
    ;





    share|improve this answer






















    • Thanks for the quick reply! I forgot to mention the target element is actualy an icon so when i use your code it just delete the little bin. I tried target.parentNode.removeChild(target.parentNode); but it didnt work so im not really sure how to target it
      – Jared
      Nov 12 '18 at 19:25











    • @Jared What does your list look like markup wise?
      – zfrisch
      Nov 12 '18 at 19:29










    • <ul id ="shape-list" class="list-group mt-3 mb-3"> <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li> </ul>
      – Jared
      Nov 12 '18 at 19:33










    • @Jared Updated!
      – zfrisch
      Nov 12 '18 at 19:37






    • 1




      I managed to get it sorted. The problem was i was targeting the li and not the ul. I changed that last line to shapeList.removeChild(target.parentNode) and its working how i want it :) Thanks fro the help though!
      – Jared
      Nov 12 '18 at 19:39















    0














    Based on your markup:



    <ul id ="shape-list" class="list-group mt-3 mb-3"> 
    <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li>
    </ul>


    You can simply use the Node method removeChild. This is assuming that the i tag is the target in your event.



     target.parentNode.parentNode.removeChild(target.parentNode);


    And inside of your code:



    // Delete shape from ul event
    shapeList.onclick = function (event)
    var shapesArray = shapesCtrl.getShapesArray();
    var target = event.target; // Getting which <li> was clicked
    var id = target.parentNode.id; // Getting the value of the li that was clicked
    canvasCtrl.deleteShape(shapesArray, id); // Deleting the targeted element from the array

    var li = shapeList.childNodes;

    target.parentNode.parentNode.removeChild(target.parentNode);
    ;





    share|improve this answer






















    • Thanks for the quick reply! I forgot to mention the target element is actualy an icon so when i use your code it just delete the little bin. I tried target.parentNode.removeChild(target.parentNode); but it didnt work so im not really sure how to target it
      – Jared
      Nov 12 '18 at 19:25











    • @Jared What does your list look like markup wise?
      – zfrisch
      Nov 12 '18 at 19:29










    • <ul id ="shape-list" class="list-group mt-3 mb-3"> <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li> </ul>
      – Jared
      Nov 12 '18 at 19:33










    • @Jared Updated!
      – zfrisch
      Nov 12 '18 at 19:37






    • 1




      I managed to get it sorted. The problem was i was targeting the li and not the ul. I changed that last line to shapeList.removeChild(target.parentNode) and its working how i want it :) Thanks fro the help though!
      – Jared
      Nov 12 '18 at 19:39













    0












    0








    0






    Based on your markup:



    <ul id ="shape-list" class="list-group mt-3 mb-3"> 
    <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li>
    </ul>


    You can simply use the Node method removeChild. This is assuming that the i tag is the target in your event.



     target.parentNode.parentNode.removeChild(target.parentNode);


    And inside of your code:



    // Delete shape from ul event
    shapeList.onclick = function (event)
    var shapesArray = shapesCtrl.getShapesArray();
    var target = event.target; // Getting which <li> was clicked
    var id = target.parentNode.id; // Getting the value of the li that was clicked
    canvasCtrl.deleteShape(shapesArray, id); // Deleting the targeted element from the array

    var li = shapeList.childNodes;

    target.parentNode.parentNode.removeChild(target.parentNode);
    ;





    share|improve this answer














    Based on your markup:



    <ul id ="shape-list" class="list-group mt-3 mb-3"> 
    <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li>
    </ul>


    You can simply use the Node method removeChild. This is assuming that the i tag is the target in your event.



     target.parentNode.parentNode.removeChild(target.parentNode);


    And inside of your code:



    // Delete shape from ul event
    shapeList.onclick = function (event)
    var shapesArray = shapesCtrl.getShapesArray();
    var target = event.target; // Getting which <li> was clicked
    var id = target.parentNode.id; // Getting the value of the li that was clicked
    canvasCtrl.deleteShape(shapesArray, id); // Deleting the targeted element from the array

    var li = shapeList.childNodes;

    target.parentNode.parentNode.removeChild(target.parentNode);
    ;






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 '18 at 19:37

























    answered Nov 12 '18 at 19:16









    zfrisch

    4,42311024




    4,42311024











    • Thanks for the quick reply! I forgot to mention the target element is actualy an icon so when i use your code it just delete the little bin. I tried target.parentNode.removeChild(target.parentNode); but it didnt work so im not really sure how to target it
      – Jared
      Nov 12 '18 at 19:25











    • @Jared What does your list look like markup wise?
      – zfrisch
      Nov 12 '18 at 19:29










    • <ul id ="shape-list" class="list-group mt-3 mb-3"> <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li> </ul>
      – Jared
      Nov 12 '18 at 19:33










    • @Jared Updated!
      – zfrisch
      Nov 12 '18 at 19:37






    • 1




      I managed to get it sorted. The problem was i was targeting the li and not the ul. I changed that last line to shapeList.removeChild(target.parentNode) and its working how i want it :) Thanks fro the help though!
      – Jared
      Nov 12 '18 at 19:39
















    • Thanks for the quick reply! I forgot to mention the target element is actualy an icon so when i use your code it just delete the little bin. I tried target.parentNode.removeChild(target.parentNode); but it didnt work so im not really sure how to target it
      – Jared
      Nov 12 '18 at 19:25











    • @Jared What does your list look like markup wise?
      – zfrisch
      Nov 12 '18 at 19:29










    • <ul id ="shape-list" class="list-group mt-3 mb-3"> <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li> </ul>
      – Jared
      Nov 12 '18 at 19:33










    • @Jared Updated!
      – zfrisch
      Nov 12 '18 at 19:37






    • 1




      I managed to get it sorted. The problem was i was targeting the li and not the ul. I changed that last line to shapeList.removeChild(target.parentNode) and its working how i want it :) Thanks fro the help though!
      – Jared
      Nov 12 '18 at 19:39















    Thanks for the quick reply! I forgot to mention the target element is actualy an icon so when i use your code it just delete the little bin. I tried target.parentNode.removeChild(target.parentNode); but it didnt work so im not really sure how to target it
    – Jared
    Nov 12 '18 at 19:25





    Thanks for the quick reply! I forgot to mention the target element is actualy an icon so when i use your code it just delete the little bin. I tried target.parentNode.removeChild(target.parentNode); but it didnt work so im not really sure how to target it
    – Jared
    Nov 12 '18 at 19:25













    @Jared What does your list look like markup wise?
    – zfrisch
    Nov 12 '18 at 19:29




    @Jared What does your list look like markup wise?
    – zfrisch
    Nov 12 '18 at 19:29












    <ul id ="shape-list" class="list-group mt-3 mb-3"> <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li> </ul>
    – Jared
    Nov 12 '18 at 19:33




    <ul id ="shape-list" class="list-group mt-3 mb-3"> <li>Link: url.com <i class="delete-icon fas fa-trash-alt"></i> </li> </ul>
    – Jared
    Nov 12 '18 at 19:33












    @Jared Updated!
    – zfrisch
    Nov 12 '18 at 19:37




    @Jared Updated!
    – zfrisch
    Nov 12 '18 at 19:37




    1




    1




    I managed to get it sorted. The problem was i was targeting the li and not the ul. I changed that last line to shapeList.removeChild(target.parentNode) and its working how i want it :) Thanks fro the help though!
    – Jared
    Nov 12 '18 at 19:39




    I managed to get it sorted. The problem was i was targeting the li and not the ul. I changed that last line to shapeList.removeChild(target.parentNode) and its working how i want it :) Thanks fro the help though!
    – Jared
    Nov 12 '18 at 19:39













    2














    I don't know what your list looks like, but you should be able to adopt this accordingly.






    const lis = [...document.querySelectorAll('.this-list li')];

    for (const li of lis)
    li.addEventListener('click', function()
    this.parentNode.removeChild(this);
    )

    <ul class="this-list">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
    </ul>





    If you prefer a delegate listener on the ul itself, here you go:






    const ul = document.querySelector('.this-list');

    ul.addEventListener('click', function(e)
    this.removeChild(e.target);
    )

    <ul class="this-list">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
    </ul>





    This has the advantage that it works for dynamically created list items, too:






    const ul = document.querySelector('.this-list');

    for (let i=0; i<5; i++)
    let li = document.createElement('li');
    li.textContent = `Item $i+1`;
    ul.appendChild(li);


    ul.addEventListener('click', function(e)
    this.removeChild(e.target);
    )

    <ul class="this-list"></ul>








    share|improve this answer



























      2














      I don't know what your list looks like, but you should be able to adopt this accordingly.






      const lis = [...document.querySelectorAll('.this-list li')];

      for (const li of lis)
      li.addEventListener('click', function()
      this.parentNode.removeChild(this);
      )

      <ul class="this-list">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
      <li>Item 5</li>
      </ul>





      If you prefer a delegate listener on the ul itself, here you go:






      const ul = document.querySelector('.this-list');

      ul.addEventListener('click', function(e)
      this.removeChild(e.target);
      )

      <ul class="this-list">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
      <li>Item 5</li>
      </ul>





      This has the advantage that it works for dynamically created list items, too:






      const ul = document.querySelector('.this-list');

      for (let i=0; i<5; i++)
      let li = document.createElement('li');
      li.textContent = `Item $i+1`;
      ul.appendChild(li);


      ul.addEventListener('click', function(e)
      this.removeChild(e.target);
      )

      <ul class="this-list"></ul>








      share|improve this answer

























        2












        2








        2






        I don't know what your list looks like, but you should be able to adopt this accordingly.






        const lis = [...document.querySelectorAll('.this-list li')];

        for (const li of lis)
        li.addEventListener('click', function()
        this.parentNode.removeChild(this);
        )

        <ul class="this-list">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        </ul>





        If you prefer a delegate listener on the ul itself, here you go:






        const ul = document.querySelector('.this-list');

        ul.addEventListener('click', function(e)
        this.removeChild(e.target);
        )

        <ul class="this-list">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        </ul>





        This has the advantage that it works for dynamically created list items, too:






        const ul = document.querySelector('.this-list');

        for (let i=0; i<5; i++)
        let li = document.createElement('li');
        li.textContent = `Item $i+1`;
        ul.appendChild(li);


        ul.addEventListener('click', function(e)
        this.removeChild(e.target);
        )

        <ul class="this-list"></ul>








        share|improve this answer














        I don't know what your list looks like, but you should be able to adopt this accordingly.






        const lis = [...document.querySelectorAll('.this-list li')];

        for (const li of lis)
        li.addEventListener('click', function()
        this.parentNode.removeChild(this);
        )

        <ul class="this-list">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        </ul>





        If you prefer a delegate listener on the ul itself, here you go:






        const ul = document.querySelector('.this-list');

        ul.addEventListener('click', function(e)
        this.removeChild(e.target);
        )

        <ul class="this-list">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        </ul>





        This has the advantage that it works for dynamically created list items, too:






        const ul = document.querySelector('.this-list');

        for (let i=0; i<5; i++)
        let li = document.createElement('li');
        li.textContent = `Item $i+1`;
        ul.appendChild(li);


        ul.addEventListener('click', function(e)
        this.removeChild(e.target);
        )

        <ul class="this-list"></ul>








        const lis = [...document.querySelectorAll('.this-list li')];

        for (const li of lis)
        li.addEventListener('click', function()
        this.parentNode.removeChild(this);
        )

        <ul class="this-list">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        </ul>





        const lis = [...document.querySelectorAll('.this-list li')];

        for (const li of lis)
        li.addEventListener('click', function()
        this.parentNode.removeChild(this);
        )

        <ul class="this-list">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        </ul>





        const ul = document.querySelector('.this-list');

        ul.addEventListener('click', function(e)
        this.removeChild(e.target);
        )

        <ul class="this-list">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        </ul>





        const ul = document.querySelector('.this-list');

        ul.addEventListener('click', function(e)
        this.removeChild(e.target);
        )

        <ul class="this-list">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        </ul>





        const ul = document.querySelector('.this-list');

        for (let i=0; i<5; i++)
        let li = document.createElement('li');
        li.textContent = `Item $i+1`;
        ul.appendChild(li);


        ul.addEventListener('click', function(e)
        this.removeChild(e.target);
        )

        <ul class="this-list"></ul>





        const ul = document.querySelector('.this-list');

        for (let i=0; i<5; i++)
        let li = document.createElement('li');
        li.textContent = `Item $i+1`;
        ul.appendChild(li);


        ul.addEventListener('click', function(e)
        this.removeChild(e.target);
        )

        <ul class="this-list"></ul>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 19:27

























        answered Nov 12 '18 at 19:19









        connexo

        20.5k73554




        20.5k73554













            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto