I’m having some issues selecting elements that jquery clones. Below is what I have. I can’t seem to select the .remove-container class anchor from the cloned versions. The only one that I can select is the original.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
<div id="primary">
<div class="quote-container">
<label>Quote </label>
<textarea rows=5></textarea>
<label>Author</label>
<input type="text" name="author" />
<a href="#remove" class="remove-container">Remove</a>
</div>
<a href="#add" class="add-container">Add</a>
</div>
<script>
$(function() {
$(".add-container").on("click", function(e){
e.preventDefault();
$(".quote-container").eq(0).clone().insertBefore(".add-container");
var qContainer = $(".quote-container").length;
for (i=0; i<qContainer; i++) {
if ($(".quote-container").eq(i).find("label").html() === "Quote "+(i+1)) {
} else {
$(".quote-container").eq(i).find("label").eq(0).replaceWith("<label>Quote "+(i+1)+"</label>");
}
}
});
$(".remove-container").on("click", function(e) {
e.preventDefault();
console.log($(this));
});
});
</script>
</body>
</html>
When I changed your remove link jQuery to this everything worked (as I think you’re wanting it to):
Fiddle: http://jsfiddle.net/gromer/gw3mq/
One thing to know is if you remove all quote rows, clicking the add link won’t do anything, since it won’t have anything to clone anymore.