Of course, just give a common class to each dynamically added element, in this case .dynamic. Then, when another button is pressed, any instance of this class is simply deleted.
var counter = 1;
$("#button1").click(function(){
$("<div/>", {
"class": "dynamic test" + (counter++),
text: "",
}).resizable().draggable()
.appendTo("body");
});
$("#button2").click(function(){
$(".dynamic").remove();
});
source
share