1

If I make that remove button with id attribute it isnt working. When I make it with class attribute it is working. Why its not working with id?

$(function() {
  $("#btn").on("click", function() {
    var val = $("#yzbr").val();
    if (val !== '') {
      var elem = $("<li></li>").text(val);
      $(elem).append("<button id='rem'>REMOVE</button>");
      $("#ekl").append(elem);
      $("input").val("");
      $("#rem").on("click", function() {
        $(this).parent().remove();
      })
    }
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>list</h1>
<input type="text" id="yzbr">
<button id="btn">add</button>
<ol id="ekl"></ol>
Barmar
  • 596,455
  • 48
  • 393
  • 495
ahmeteksi
  • 13
  • 2
  • 1
    The answer to your question is whenever more than one DOM element has same id any action applied to that id will impact only on last occurrence, Same is happening in you code only last element is getting removed whatever remove button you click. As per definition **ID should be uniquely assigned to DOM elements.**
    – amit wadhwani Aug 14 '17 at 22:28
  • Also, if you do it with a class, every time you add a new element, you're adding the event handler multiple times on all the previous elements. See https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements for the proper way to bind events on elements that you add dynamically. – Barmar Aug 14 '17 at 23:44