when I click a button, i appended (inserted) after it a div with new div, and i donot want to give id to the new div, because i want this function could be use by other button/anchor/etc.
$("#buttonA").click(function(){
$("#div1").after("<div>bla bla bla</div>");
});
and then by an event i want to remove the inserted div.
what’s the best way to remove inserted div? … i still cant make it generic, i must apply ID on the new inserted div, and then to remove it i use
$("#newid").remove()
First mark the inserted element with a tag (in this case, the
insertedclass):Then look for the inserted element using the selector of your choice:
You could also remove every
divinserted this way:If your intention is to allow many different events to create many different
divs all inside the same container, and then remove one particular div, then you are going to need to either generate unique ids for eachdivand map them to the callers for later reference, or simply map the createddivelement to the callers for later reference. It’s hard to make a strong suggestion without knowing more about your use case.