javascript - Removing dynamically inserted html with jQuery - Stack Overflow

I'm trying to dynamically create and remove items from a list, itworks just fine... sort of, I ca

I'm trying to dynamically create and remove items from a list, it works just fine... sort of, I can remove items, and create items, but once an item has been created, I cannot remove it again, but I can remove the items present when the page loads.

Here is my code

<div class="list">
    <div class="item">
        <input type="text" value="" />
        <a href="" class="removeitem">Remove this item</a>
    </div>
    <div class="item">
        <input type="text" value="" />
        <a href="" class="removeitem">Remove this item</a>
    </div>
    <a href="" class="additem">Add item to list</a>
</div>

<script type="text/javascript">
// Add item to list
$('.additem').click(function(){
    var template = $($(this).prev().get(0)).clone();
    template.insertBefore($(this));
    return false;
});

// Remove item from list
$('.removeitem').click(function(){
    $(this).prev().parent().remove();
    return false;
});
</script>

I can remove the 2 original items, but when I create new ones, they cannot be removed

I'm trying to dynamically create and remove items from a list, it works just fine... sort of, I can remove items, and create items, but once an item has been created, I cannot remove it again, but I can remove the items present when the page loads.

Here is my code

<div class="list">
    <div class="item">
        <input type="text" value="" />
        <a href="" class="removeitem">Remove this item</a>
    </div>
    <div class="item">
        <input type="text" value="" />
        <a href="" class="removeitem">Remove this item</a>
    </div>
    <a href="" class="additem">Add item to list</a>
</div>

<script type="text/javascript">
// Add item to list
$('.additem').click(function(){
    var template = $($(this).prev().get(0)).clone();
    template.insertBefore($(this));
    return false;
});

// Remove item from list
$('.removeitem').click(function(){
    $(this).prev().parent().remove();
    return false;
});
</script>

I can remove the 2 original items, but when I create new ones, they cannot be removed

Share Improve this question edited Jun 4, 2009 at 11:43 Garry Shutler 32.7k13 gold badges89 silver badges120 bronze badges asked Jun 4, 2009 at 11:34 kristian nissenkristian nissen 2,9175 gold badges46 silver badges71 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

You need to use live events rather than the normal style you are using at the moment.

The click events are bound on load, at which point only the original items are present to be bound.

You would use live events like this:

<script type="text/javascript">
// Add item to list
$('.additem').click(function(){
    var template = $($(this).prev().get(0)).clone();
    template.insertBefore($(this));
    return false;
});

// Remove item from list
$('.removeitem').live("click", function(){
    $(this).prev().parent().remove();
    return false;
});
</script>

There is an overhead to using live events (it has to monitor all events in the DOM and check if they match I believe). Therefore, only use them when necessary.

Change:

$('.removeitem').click(function(){
    $(this).prev().parent().remove();
    return false;
});

to:

$('.removeitem').live("click", function(){
    $(this).prev().parent().remove();
    return false;
});

Does this work?

$('.removeitem').live("click", function(){
$(this).parent('div.item').remove();
return false;
});

Other using .live() is a good idea, as you already know.

The other option to live events would be to attach the click event to your template:

$('.additem').click(function(){
   var template = $($(this).prev().get(0)).clone();
   template.insertBefore($(this));
   $(template).click( function() { 
      $(this).prev().parent().remove();
      return false;
   };
   return false;
});

That way you aren't dealing with the overheads of live events. If you do choose to do this you might want to pull the remove code into a function eg:

<script type="text/javascript">
  function removeitem() {
      $(this).prev().parent().remove();
      return false;
  }
  // Add item to list
  $('.additem').click(function(){
    var template = $($(this).prev().get(0)).clone();
    template.insertBefore($(this));
    $(template).click(removeitem);
    return false;
  });
  // Remove item from list
  $('.removeitem').click(removeitem);
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744203004a4562969.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信