how to make a list of items that can be added to or removed using jquery? -
i'm trying create dynamic item list can added , removed animations. problem have new items can't removed. ideas on i'm doing wrong?
http://jsfiddle.net/waspinator/nz9y2/1/
html
<button class="add-item-button"> add item </button> <div id="list"> <!-- prepend new item here --> <div class="item"> first item <span class="remove-item">x</span> </div> <div class="item"> second item <span class="remove-item">x</span> </div> </div>
css:
#list{ width: 140px; } .remove-item{ font-family: sans-serif; float: right; margin-top: -6px; cursor: pointer; } .item{ color: white; background: cornflowerblue; margin-bottom: 4px; padding: 4px; border-radius: 4px; } .add-item-button{ margin-bottom: 10px; width: 140px; }
js
var item_number = 1; $('.remove-item').click(function(){ $(this).parent().animate({opacity: 0}, function () { $(this).slideup(); }); }); $('.add-item-button').click(function() { var new_item = '<div class="item">' + 'new item ' + item_number + '<span class="remove-item">x</span>' + '</div>' $(new_item).prependto('#list').hide().slidedown(); item_number = item_number + 1; });
bind #list
in dom, select on .remove-item
class. here fiddle: http://jsfiddle.net/nz9y2/3/
var item_number = 1; var remove = function() { $(this).parent().animate({opacity: 0}, function () { $(this).slideup(); }); } $('#list').on('click', '.remove-item', remove); $('.add-item-button').click(function() { var new_item = '<div class="item">' + 'new item ' + item_number + '<span class="remove-item">x</span>' + '</div>' $(new_item).prependto('#list').hide().slidedown(); item_number = item_number + 1; });
Comments
Post a Comment