I've got a content editable div like this:
<div id="editor" contenteditable="true">
And I've got two buttons:
<div id="button1" onclick="javascript()">
<div id="button2" onclick="javascript()">
Both buttons call the same javascript-function, so this function will be repeated every time you click on one of the buttons. For example:
When I click on button1, the script adds an Eventlistener to the editor. I use this to save the content of the editor on certain keystrokes.
When I click on button2, the eventlistener needs to be removed and a new one needs to be added. How can I do that?
I already tried this:
function javascript() {
// remove the eventlistener
editable.removeEventListener('keydown', keydownsave, true);
var editable = document.getElementById('editor');
editable.addEventListener('keydown', keydownsave, true);
keydownsave = function(e) {
// execute only if certain keys are pressed.
var toets = e.keyCode;
var ctrls = e.ctrlKey;
if (toets == 32 || toets == 13 || ctrls == true && toets == 83)
But this didn't work.