Wenn Sie unter dem JSFiddle gehen. Fügen Sie zwei Elemente hinzu und drücken Sie "Fertig" für eines der Elemente in der Konsole, um dieselbe Schaltfläche zweimal auszubuchen. Ich kann nicht herausfinden warum.Schaltfläche wird zweimal ausgelöst, wenn geklickt wird
Was mache ich hier falsch? :)
https://jsfiddle.net/7k84p3oz/6/
HTML
<input id="inputAdd" type="text" name="" value="">
<button id="submitItem" type="button" name="button">Add</button>
<!-- Start of list -->
<ul id="listContent"></ul>
<!-- End of list -->
<!-- Start of completed -->
<div id="completed">
<h4>Completed</h4>
</div>
<!-- End of completed -->
JS
$(document).ready(function(){
(function() {
var itemTracker = {
// init
init: function() {
this.bindEvents();
},
// cacheDom
cacheDom: {
inputAdd: $('#inputAdd'),
submitAdd: $('#submitItem'),
listContent: $('#listContent'),
completed: $('#completed')
},
// Add item
addItem: function() {
var itemValue = this.cacheDom.inputAdd.val();
var listItem = $("<li></li>");
var buttonRemove = '<button class="remove">Remove</button>';
var buttonComplete = '<button class="complete">Complete</button>';
listItem.append(itemValue);
listItem.append(buttonRemove);
listItem.append(buttonComplete);
var itemContent = this.cacheDom.listContent.append(listItem);
this.cacheDom.inputAdd.val("");
// Remove item
var remove = $('.remove');
remove.on("click", function(e){
$(e.target).closest("li").hide();
});
var complete = $(".complete");
// Complete item
var completedItem = function(e) {
console.log(this);
// var childParent = $(this).parent();
// var rootParent = childParent.parent();
// var parentId = rootParent.prop('id');
//
// if(parentId === "listContent") {
// $('#completed').append(listItem);
// } else {
// $('#listContent').append(listItem);
// }
};
complete.on("click", completedItem);
},
// Bind Events
bindEvents: function() {
this.cacheDom.submitAdd.on("click", this.addItem.bind(this));
}
};
itemTracker.init();
})();
});
nicht reproduzieren kann, sehe ich nur ein Protokoll. –