First, my JSFiddle
I have dynamically created content that is supposed to delete when they are clicked. I am using the variable emma to create a click function for each div that is clicked.
Every time I click on a content div, the amount of contents is reduced. As such, I want my emma count to reduce corresponding to its place in the DOM.
So if I click on Content 1, whose emma value in the loop is 1, Content 1 deletes, and in its place is Content 2. I want Content 2 to take its place and have an emma value of 1. Instead, if I click on Content 2 after deleting Content 1, then the value of emma on Content 2 is still 2.
I am ultimately using the emma variable for CRUD features.
How might I adjust to keep my emma value updated after each of my content is deleted?
function digiteditcall(){
for (emma = 0; emma < mar; emma++){
(function(emma){
document.addEventListener('click', function(e){
let tenille = "content"+emma;
if ( (e.target && e.target.id == tenille) || (e.target && e.target.parentNode.id == tenille) ){
maincontainer.removeChild(document.getElementById(tenille));
mar--;
console.log(emma);
}
});
})(emma);
}
}
Again, here is my JSFiddle
letand you don't need the closure. This is an archaic pattern -letwas added to the spec so you don't have to do this anymore.... that said, I really don't know what you mean by "keep the emma variable updated"...