0

i got this code written a few minutes ago I was wondering if why won't the item be removed.

(function($) {
  $('#btn1').click(function() {
    var textVal = $('#input').val();
    if (textVal != "") {
      $('#list').append('<li class="class1" id="' + textVal + '">' + textVal + '<input type="hidden" name="Tags" value="' + textVal + '" /> <input type="button" class="remove-item" value="X"></input></li> ');
    }
  });
  $('.remove-item').click(function () {
    $('.list #' + textVal).remove()
  });
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="input" type="text" />
<input type="button" id="btn1" value="Add" />
<ul id="list"></ul>
Nhan
  • 3,595
  • 6
  • 30
  • 38
Nevi
  • 328
  • 2
  • 8
  • 18

3 Answers3

2

You need to add an .on() over body and check if the clicked class is .remove-item and remove() it.

(function($) {
  $('#btn1').click(function() {
    var textVal = $('#input').val()
    if (textVal != "") {
      $('#list').append('<li class="class1" id="' + textVal + '">' + textVal + '<input type="hidden" name="Tags" value="' + textVal + '" /> <input type="button" class="remove-item" value="X"></input></li> ');
    }
  });
  $('body').on('click', '.remove-item', function(e) {
    $(this).parent().remove()
  });
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="input" type="text" />
<input type="button" id="btn1" value="Add" />

<ul id="list">
</ul>
Pranesh Ravi
  • 18,642
  • 9
  • 46
  • 70
1

Try this code :

  var textVal;
    $('#btn1').click(function(){
      textVal = $('#input').val()
        if (textVal != ""){
 $('#list').append('<li class="class1" id="' + textVal + '">' + textVal + '<input type="hidden" name="Tags" value="' + textVal + '" /> <input type="button" class="remove-item" id='+textVal+' value="X"></input></li> ');
          
    $('#'+ textVal +' .remove-item' ).click(function (e){
    $(e.target).parent().remove();
    });
          
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="input" type="text" />
    <input type="button" id="btn1" value="Add" />

    <ul id="list">
    </ul>

Also i would suggest to use Backbone.js (Based on MVC) for such Apps

0

Please check this. No need to grab text for current li. Instead you can search for parent li and can remove it as follows:

(function($) {
  $('#btn1').click(function() {
    var textVal = $('#input').val()
    if (textVal != "") {
      $('#list').append('<li class="class1" id="' + textVal + '">' + textVal + '<input type="hidden" name="Tags" value="' + textVal + '" /> <input type="button" class="remove-item" value="X"></input></li> ');
    }
  });


  $("#list").on('click', '.remove-item', function() {
    $(this).closest("li").remove()

  });

})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="input" type="text" />
<input type="button" id="btn1" value="Add" />

<ul id="list">
</ul>
vijayP
  • 11,432
  • 5
  • 25
  • 40