I am trying to remove an event listener with parameters. I've understood that passing arguments when assigning a listener wouldn't work, so I finally worked a way to remove the arguments for the listener assignment. But it still not working.
I'm really not sure where to look next.
Here is the code (I have removed everything that has nothing to do with the issue)
let activeItemOnTheMove = false;
activeItem.addEventListener('click', e=>{
const defineOnMouseMove = () => { onMouseMove(activeItem) };
if(!activeItemOnTheMove){
document.addEventListener('mousemove', defineOnMouseMove);
activeItemOnTheMove = true;
}else{
document.removeEventListener('mousemove', defineOnMouseMove);
activeItemOnTheMove = false;
}
})
function onMouseMove(activeItem){
activeItem.style.position ='absolute';
activeItem.style.left = event.pageX + 'px';
activeItem.style.top = event.pageY + 'px';
activeItem.style.transform = 'translate(-50%,-50%)'
}
(I am using vanilla javascript)
I have read these answers, this is how I understood that I couldn't use an anonymous function. But I am not sure I understand the fundamental of why it doesn't work.