I have editable html page so that user can copy and paste html elements and also dynamically create elements as well. I have hooked some event listener in these element but for obvious i need to hooked event listener in those dynamic added elements. I come up with the solution is that, every time user focus on these element, a event fire and the required event listener hooked up with that specific element.
$('body').on('focusin', '.propclass', function() {
autocomplete(this,idsprop);
});
Everything is just working fine except that every time when I hooked event listener, it don't discard previous event listener but it add one more event listener for that element so for eexample, if I focus 5 times on that element, it hooked and call 5 times to that element.
I have tried to remove previous event listener by:
inp.removeEventListener("input",call_input,true);
But it didn't work.
function autocomplete(inp, arr) {
inp.removeEventListener("input",call_input,true);
inp.removeEventListener("keydown",call_keydown,true);
inp.addEventListener("input", function(e) {
call_input(e);
});
inp.addEventListener("keydown", function(e) {
call_keydown(e);
});
};
So every time autocomplete call, firstly I try to remove the listener then add new listener.
Is there is any way to delete previous event listener? Is my approach is correct or there is some better way to do this?