Alright, first time posting here. I have been looking at posts regarding this issue for days now and I can't find a fix.
I have a matrix of images (basically arranged spheres) with unique id's. I want to add different functionalities to said spheres. So I proceed to add event listeners. All good so far. After the first event is triggered I want all the spheres to change their event listener, so I proceed to remove all event listeners and add new ones. I cannot remove the event listeners.
Adding event:
for (let i = 0; i < 11; i++) {
for (let j = 0; j < 11; j++) {
if ((i > 1 && i < 9 && j > 1 && j < 9) && checkCorners(i, j) === true) {
let ball = document.getElementById(i.toString() + j.toString());
ball.addEventListener('click', removeBall(i, j));
}
}
}
removeBall():
var removeBall = function(i, j) {
return function curried_func(ii, jj) {
let ball = document.getElementById(i.toString() + j.toString());
ball.src = "./public/assets/ball_empty.png";
}
}
I'm using the curried function method because it's the last method I have tried, besides retrieving object data from the e (event) argument, which did not seem to work as e was undefined. Or using a handler. Or using binding.
Removing event:
for (let i = 0; i < 11; i++) {
for (let j = 0; j < 11; j++) {
if ((i > 1 && i < 9 && j > 1 && j < 9) && checkCorners(i, j) === true) {
let ball = document.getElementById(i.toString() + j.toString());
// ball.somethingToRevmoveEvent?
}
}
}
I do feel the need to mention it's also my first javascript serious project so if anyone has a better solution or workaround (instead of adding and removing events 100 times for functionalities) please do share.
matrix for reference -the user should remove 1 ball , at which point the spot turns black, and then any ball can jump over another to fill the empty spot (the "jumped over ball" gets removed). I think it's a chinese checker puzzle of some kind.