0

In my html I have this code for the list-group:

<ul class="list-group" name="list-group" id="list-group">

I populate this list group via onclick method with this on my js: where a = value of a dropdown button;

 $('#list-group').append('<li>' + a  +'<button id="delete">delete</button></li>');

or

$('#list-group').append('<button type="button" id="delete" class="list-group-item list-group-item-action">' + a + '<span class="pull-right"><span class="glyphicon glyphicon-erase" aria-hidden="true"></span></span></button>');

How can I remove the appended list item dynamically? I tried to delete or remove it with this code:

$(".delete").on('click',function () {
    $(this).closest("li").remove();
    alert("clicked");
   });

Thanks in advance!

Mistuu
  • 9
  • 3
  • Use `#delete` instead of `.delete` as you have assigned `id="delete"` and not class – AHMED SAJJAD Feb 19 '21 at 12:02
  • 1
    @AHMEDSAJJAD if she is populating the button in list then there will be duplicate IDs. please use className in your html code `$('#list-group').append('
  • ' + a +'
  • ');` – kunal panchal Feb 19 '21 at 12:04
  • Does this answer your question? [How to add a list item to an existing unordered list?](https://stackoverflow.com/questions/1145208/how-to-add-a-list-item-to-an-existing-unordered-list) – ℛɑƒæĿᴿᴹᴿ Feb 19 '21 at 12:05
  • @kunalpanchal good catch, my bad for overlooking it – AHMED SAJJAD Feb 19 '21 at 12:05