Reputation: 2509
HTML:
<div id='task-header'>
</div>
<div id='tasks'>
<a href="#" id ="add">Add</a>
<ul>
<li class ="editable">
<form class='task'>
<input class='textfield' type='text' />
</form>
<a href="#" class="delete">Delete this row</a>
</li>
</ul>
</div>
<div id='task-footer'></div>
jQuery:
var item = $("#tasks ul li:eq(0)").clone(true); // need to clone for new add
$('#add').click(function() {
var taskItem = item.clone(true);
$('#tasks ul').append(taskItem);
taskItem.find(':input:text').val("");
return false;
});
$('body').on('click', '.delete', function() {
$(this).parent('li').remove();
return false;
});
$(".editable").hover(function() {
$(this).addClass("editHover");
}, function() {
$(this).removeClass("editHover");
});
JSFiddle: http://jsfiddle.net/t67yJ/
I set my clone(true)
parameter, but I still cannot get my cloned <li>
's to have the hover effect. What should I do?
Thanks.
Upvotes: 2
Views: 1977
Reputation: 145428
In order not to clone element twice, you can remove one extra clone
in the first line and attach handlers in delegated-events approach:
var item = $("#tasks ul li:eq(0)");
$("body").on("hover", ".editable", function() {
$(this).toggleClass("editHover");
}).on("click", "#add", function() {
var taskItem = item.clone(true).removeClass("editHover");
$("#tasks ul").append(taskItem);
taskItem.find(":input:text").val("");
return false;
}).on("click", ".delete", function() {
$(this).parent("li").remove();
return false;
});
DEMO: http://jsfiddle.net/t67yJ/7/
Upvotes: 0
Reputation: 87073
$(".editable").hover(function() {
$(this).addClass("editHover");
}, function() {
$(this).removeClass("editHover");
});
var item = $("#tasks ul li:eq(0)").clone(true); // just change the place
// of first clone statement
$('#add').click(function() {
var taskItem = item.clone(true);
$('#tasks ul').append(taskItem);
taskItem.find(':input:text').val("");
return false;
});
$('body').on('click', '.delete', function() {
$(this).parent('li').remove();
return false;
});
var item = $("#tasks ul li:eq(0)"); // remove clone(true) from here
$('#add').click(function() {
var taskItem = item.clone(true);
$('#tasks ul').append(taskItem);
taskItem.find(':input:text').val("");
return false;
});
$('body').on('click', '.delete', function() {
$(this).parent('li').remove();
return false;
});
// use event delegation
$("#tasks").on('hover', '.editable', function(e) {
if (e.type == 'mouseenter') $(this).addClass("editHover");
else $(this).removeClass("editHover");
});
Upvotes: 2
Reputation: 79830
Simply move the clone call below the handler like below,
$(".editable").hover(function() {
$(this).addClass("editHover");
}, function() {
$(this).removeClass("editHover");
});
var item = $("#tasks ul li:eq(0)").clone(true); // need to clone for new add
Upvotes: 2
Reputation: 70169
The event listeners aren't defined yet when you clone the element. Move var item = $("#tasks ul li:eq(0)").clone(true);
to the end of your script.
Or use the .on
/.live
event delegation and you won't have to clone the listeners, as coder said.
Upvotes: 0