I want to create dynamically rows with jquery and for each new row adding a counting number from 1 to 2 etc. but when i click on add row the first click goes good, but then after that it counts on the existing ones and i only need 1, 2, 3, 4, 5, etc. What am i doing wrong ?
Fiddle: Click
HTML:
<div class="row">I'm a row<p></p></div>
<a href="#" class="add_row">Add Row</a>
CSS:
.row {
width: 100px;
height: 100px;
padding: 10px;
margin: 5px;
background-color: slateGrey;
float: left;
}
a.add_row {
float: left;
}
JQUERY:
$("a.add_row").click(function() {
var $row = $("body .row").first().html();
var $remove_row = ("<a href='#' class='remove_row'>remove</a>");
$(this).before("<div class='row'>"+ $row +"</div>");
$(this).prev().append($remove_row);
var count = 0;
$(".remove_row").click(function() {
var $remove = $(this).parent();
$remove.remove();
return false;
});
$(".row").each(function() {
count++;
$(this).append(count);
});
return false;
});