I try to remove an event listener with Javascript, but it seems not working.
I need to pass the object event in the handler so this is why I'm using .bind().
My code :
if (sidebar) {
listItems.forEach(function(listItem) {
listItem.showSubmenu = showSubmenu;
listItem.addEventListener('click', listItem.showSubmenu.bind(this, subLists), true);
});
if (sidebarButton) {
sidebarButton.addEventListener('click', onClickOnButton.bind(this, sidebar));
}
}
if (sidebar.getAttribute('data-reduce') === 'true') {
listItems.forEach(function(listItem) {
listItem.removeEventListener('click', listItem.showSubmenu, true);
});
}
function showSubmenu(subLists, e) {
var target = e.currentTarget,
subList = target.querySelector('.sidebar__sublist');
if (subList) {
if (subList.style.display !== 'block') {
[...subLists].forEach(hideSubList);
$(subList).slideDown('slow', function() {
subList.style.display = 'block';
target.querySelector('.sidebar__list__item__link__arrow').textContent = 'keyboard_arrow_up';
});
} else {
$(subList).slideUp('slow', function() {
subList.style.display = 'none';
target.querySelector('.sidebar__list__item__link__arrow').textContent = 'keyboard_arrow_down';
});
}
}
}
Result : The event is not removed.
listItem.showSubmenuis not the same aslistItem.showSubmenu.bind(this, subLists). When youbinda new object is created to handle.event, so is there any other solution ?.on()and.off()?