I saw a lot of questions on the site for the same topic ("removeEventListener not working), but I read everything I could find, and none of them resolved my issue. I did find a few that suggested some workarounds (like creating an object in the scope that's responsible for adding and deleting listeners) but none of them really worked for me, or I had no idea how to implement them to my code specifically. This is why I'm allowing myself to post this. If someone finds a post that specifically resolves my problem, I'll remove this.
To put things briefly, I have a HTML table with "contenteditable" set to true on all "td" tags. I have a button under it which calls the deleteMode() function:
function deleteMode(){
let tds = document.getElementsByTagName('td');
if (delete_mode == true){
table.style.border = null;
for(let i = 0; i < tds.length; i++){
tds[i].style.cursor = null;
tds[i].setAttribute("contenteditable", "true");
tds[i].removeEventListener("mouseover", function(){ setDeleteBGC(i); });
tds[i].removeEventListener("mouseout", function(){ removeDeleteBGC(i); });
tds[i].removeEventListener("click", function(){ deleteEvent(i); });
}
delete_mode = false;
}
else{
table.style.border = "solid 5px red";
for(let i = 0; i < tds.length; i++){
tds[i].style.cursor = "pointer";
tds[i].removeAttribute("contenteditable");
tds[i].addEventListener("mouseover", function(){ setDeleteBGC(i); });
tds[i].addEventListener("mouseout", function(){ removeDeleteBGC(i); });
tds[i].addEventListener("click", function(){ deleteEvent(i); });
}
delete_mode = true;
}
}
(delete_mode variable is set to "false" on page load) For my eventListeners, since I had to send a parameter to the function, I used an anonymous function to call the specified function with the parameters. If I understand correctly, this is why there is a problem.
For those that need to see the full code (HTML + Javascript functions that are being called), here is a JS fiddle:
https://jsfiddle.net/67p5kLze/6/
As you can see in the fiddle, as a user "enters" "delete mode", he is able to click to empty cells 4 by 4. However, as he "exits" it, he can still empty them by clicking, and there is still the orange "preview", which I think is because the EventHandlers are not being removed.
Any help is greatly appreciated. Thanks in advance!
Following @Yousaf 's answer, I modified my code to something like this:
function deleteMode(){
let tds = document.getElementsByTagName('td');
if (delete_mode == true){
table.style.border = null;
for(let i = 0; i < tds.length; i++){
tds[i].style.cursor = null;
tds[i].setAttribute("contenteditable", "true");
bindFunc = deleteEvent.bind(null, i);
tds[i].removeEventListener("mouseover", function(){ setDeleteBGC(i); });
tds[i].removeEventListener("mouseout", function(){ removeDeleteBGC(i); });
tds[i].removeEventListener("click", bindFunc);
}
delete_mode = false;
}
else{
table.style.border = "solid 5px red";
for(let i = 0; i < tds.length; i++){
tds[i].style.cursor = "pointer";
tds[i].removeAttribute("contenteditable");
var bindFunc = deleteEvent.bind(null, i);
tds[i].addEventListener("mouseover", function(){ setDeleteBGC(i); });
tds[i].addEventListener("mouseout", function(){ removeDeleteBGC(i); });
tds[i].addEventListener("click", bindFunc);
}
delete_mode = true;
}
function deleteEvent(index){
let tds = document.getElementsByTagName('td');
console.log(index);
index = Math.floor(index/4) * 4;
let selectInCell = tds[index].getElementsByTagName('select');
selectInCell[0].selectedIndex = 0;
tds[index+1].innerHTML = '';
tds[index+2].innerHTML = '';
tds[index+3].innerHTML = '';
}
}
However, its not working, I'm thinking because the addEventListener and removeEventListener are not in a loop. It's the only way my code was different to his.
If anyone sees the mistake I'm doing, I'd appreciate the help!