Javascript “removeEventListener” not workingCreate GUID / UUID in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?
Is it acceptable for a professor to tell male students to not think that they are smarter than female students?
Is "remove commented out code" correct English?
Zip/Tar file compressed to larger size?
Ambiguity in the definition of entropy
How do conventional missiles fly?
Why didn't Boeing produce its own regional jet?
Dreadful Dastardly Diseases, or Always Atrocious Ailments
I would say: "You are another teacher", but she is a woman and I am a man
Why no variance term in Bayesian logistic regression?
What does the expression "A Mann!" means
What is the most common color to indicate the input-field is disabled?
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
Can my sorcerer use a spellbook only to collect spells and scribe scrolls, not cast?
How did the Super Star Destroyer Executor get destroyed exactly?
What method can I use to design a dungeon difficult enough that the PCs can't make it through without killing them?
Expand and Contract
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
Is the myth that if you can play one instrument, you can learn another instrument with ease true?
What's the in-universe reasoning behind sorcerers needing material components?
Short story with a alien planet, government officials must wear exploding medallions
Little known, relatively unlikely, but scientifically plausible, apocalyptic (or near apocalyptic) events
Alternative to sending password over mail?
How much of data wrangling is a data scientist's job?
Intersection Puzzle
Javascript “removeEventListener” not working
Create GUID / UUID in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?
I've tried troubleshooting my code using console.log and seeing if it will run, but it seems like it just bypasses the the removeEventListeners. I've had peers check my code and not understand what the problem is.
Javascript:
function useCodelock()
if (one == 1 && nine == 1 && five == 1 & seven == 1 && two == 1 && ninetwo == 1)
document.getElementById("main-view").style.backgroundImage="url('images/gray.png')";
document.getElementById("1").removeEventListener("click", removeImg);
document.getElementById("6").removeEventListener("click", removeImg);
document.getElementById("9").removeEventListener("click", removeImg);
document.getElementById("12").removeEventListener("click", removeImg);
document.getElementById("14").removeEventListener("click", removeImg);
document.getElementById("21").removeEventListener("click", removeImg);
document.getElementById("34").removeEventListener("click", removeImg);
document.removeEventListener("click", removeImg);
else
alert("You Must Find The Code To Use The Lock.");
HTML:
<div class="main-view-cont">
<div class="main-view" id="main-view">
<div class="grid-item"><img src="" id="1" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/9.png" id="6" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/5.png" id="9" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="" id="12" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/keypad.png" id="14" onclick="useCodelock()"></div>
<div class="grid-item"><img src="" id="21" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="" id="34" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item2"><img src="" id="i1" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i2" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i3" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i4" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i5" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i6" class="opacity1"/></div>
</div>
</div>
javascript
add a comment |
I've tried troubleshooting my code using console.log and seeing if it will run, but it seems like it just bypasses the the removeEventListeners. I've had peers check my code and not understand what the problem is.
Javascript:
function useCodelock()
if (one == 1 && nine == 1 && five == 1 & seven == 1 && two == 1 && ninetwo == 1)
document.getElementById("main-view").style.backgroundImage="url('images/gray.png')";
document.getElementById("1").removeEventListener("click", removeImg);
document.getElementById("6").removeEventListener("click", removeImg);
document.getElementById("9").removeEventListener("click", removeImg);
document.getElementById("12").removeEventListener("click", removeImg);
document.getElementById("14").removeEventListener("click", removeImg);
document.getElementById("21").removeEventListener("click", removeImg);
document.getElementById("34").removeEventListener("click", removeImg);
document.removeEventListener("click", removeImg);
else
alert("You Must Find The Code To Use The Lock.");
HTML:
<div class="main-view-cont">
<div class="main-view" id="main-view">
<div class="grid-item"><img src="" id="1" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/9.png" id="6" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/5.png" id="9" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="" id="12" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/keypad.png" id="14" onclick="useCodelock()"></div>
<div class="grid-item"><img src="" id="21" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="" id="34" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item2"><img src="" id="i1" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i2" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i3" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i4" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i5" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i6" class="opacity1"/></div>
</div>
</div>
javascript
add a comment |
I've tried troubleshooting my code using console.log and seeing if it will run, but it seems like it just bypasses the the removeEventListeners. I've had peers check my code and not understand what the problem is.
Javascript:
function useCodelock()
if (one == 1 && nine == 1 && five == 1 & seven == 1 && two == 1 && ninetwo == 1)
document.getElementById("main-view").style.backgroundImage="url('images/gray.png')";
document.getElementById("1").removeEventListener("click", removeImg);
document.getElementById("6").removeEventListener("click", removeImg);
document.getElementById("9").removeEventListener("click", removeImg);
document.getElementById("12").removeEventListener("click", removeImg);
document.getElementById("14").removeEventListener("click", removeImg);
document.getElementById("21").removeEventListener("click", removeImg);
document.getElementById("34").removeEventListener("click", removeImg);
document.removeEventListener("click", removeImg);
else
alert("You Must Find The Code To Use The Lock.");
HTML:
<div class="main-view-cont">
<div class="main-view" id="main-view">
<div class="grid-item"><img src="" id="1" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/9.png" id="6" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/5.png" id="9" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="" id="12" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/keypad.png" id="14" onclick="useCodelock()"></div>
<div class="grid-item"><img src="" id="21" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="" id="34" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item2"><img src="" id="i1" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i2" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i3" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i4" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i5" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i6" class="opacity1"/></div>
</div>
</div>
javascript
I've tried troubleshooting my code using console.log and seeing if it will run, but it seems like it just bypasses the the removeEventListeners. I've had peers check my code and not understand what the problem is.
Javascript:
function useCodelock()
if (one == 1 && nine == 1 && five == 1 & seven == 1 && two == 1 && ninetwo == 1)
document.getElementById("main-view").style.backgroundImage="url('images/gray.png')";
document.getElementById("1").removeEventListener("click", removeImg);
document.getElementById("6").removeEventListener("click", removeImg);
document.getElementById("9").removeEventListener("click", removeImg);
document.getElementById("12").removeEventListener("click", removeImg);
document.getElementById("14").removeEventListener("click", removeImg);
document.getElementById("21").removeEventListener("click", removeImg);
document.getElementById("34").removeEventListener("click", removeImg);
document.removeEventListener("click", removeImg);
else
alert("You Must Find The Code To Use The Lock.");
HTML:
<div class="main-view-cont">
<div class="main-view" id="main-view">
<div class="grid-item"><img src="" id="1" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/9.png" id="6" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/5.png" id="9" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="" id="12" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="images/keypad.png" id="14" onclick="useCodelock()"></div>
<div class="grid-item"><img src="" id="21" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item"><img src="" id="34" onclick="removeImg(this)" class="opacity25"/></div>
<div class="grid-item2"><img src="" id="i1" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i2" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i3" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i4" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i5" class="opacity1"/></div>
<div class="grid-item2"><img src="" id="i6" class="opacity1"/></div>
</div>
</div>
javascript
javascript
asked Mar 8 at 22:46
EquityEquity
132
132
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
removeEventListener
can only remove listeners that were added with addEventListener
. If you use onclick
attributes, you need to assign to that property.
function useCodelock()
if (one == 1 && nine == 1 && five == 1 & seven == 1 && two == 1 && ninetwo == 1)
document.getElementById("main-view").style.backgroundImage = "url('images/gray.png')";
document.getElementById("1").onclick = null;
document.getElementById("6").onclick = null;
document.getElementById("9").onclick = null;
document.getElementById("12").onclick = null;
document.getElementById("14").onclick = null;
document.getElementById("21").onclick = null;
document.getElementById("34").onclick = null;
document.onclick = null;
else
alert("You Must Find The Code To Use The Lock.");
I'm trying to add a secondary function after this, how would I go about that? Would I use "addEventListener" this time around? Or would there be another method?
– Equity
Mar 8 at 23:00
@Equity No you can usedocument.getElementById("button").onclick = newFunction;
to assign a new function, but you can also use addEventListener if you would rather do that.
– Nils Kähler
Mar 8 at 23:13
@Equity You can use whatever method you want. You just have to be consistent between the way you add the listener and remove it.
– Barmar
Mar 8 at 23:16
add a comment |
A call like removeListener("click", myFunc, true);
will only remove a listener that was added using addListener("click", myFunc, true);
. Here is an example:
var flag = true;
function toggle()
if (flag) document.getElementById("1").addEventListener("click", removeImg);
else document.getElementById("1").removeEventListener("click", removeImg);
flag = !flag;
function removeImg()
console.log("Clicked");
document.getElementById("toggle").addEventListener("click", toggle);
<button id="1">Test</button>
<button id="toggle">Toggle</button>
Here is a list of things you should check if it still doesn't work:
- Use the same function in the
addEventListener
as in theremoveListener
(() =>
is not the same as() =>
) - Use the same
useCapture
value (both need to betrue
or both need to befalse
) - Check that you are removing the listener from the correct element
- Check that the listener has only been added once to the element
add a comment |
You need to unbind the onclick function by setting it to null instead of using removeEventListener
.
function myFunction()
console.log('unbind me');
// if you want to unbind the function
document.getElementById("button").onclick = null;
// if you want to assign a new unction.
document.getElementById("button").onclick = newFunction;
function newFunction()
console.log("hello new");
<button id="button" onclick="myFunction()">
unbind me
</button>
You are trying to remove an eventlistener that is not present, because it is never set by addEventListener
. You have an onclick listener that is set on the DOM, so you need to unbind that function by setting it to null.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072041%2fjavascript-removeeventlistener-not-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
removeEventListener
can only remove listeners that were added with addEventListener
. If you use onclick
attributes, you need to assign to that property.
function useCodelock()
if (one == 1 && nine == 1 && five == 1 & seven == 1 && two == 1 && ninetwo == 1)
document.getElementById("main-view").style.backgroundImage = "url('images/gray.png')";
document.getElementById("1").onclick = null;
document.getElementById("6").onclick = null;
document.getElementById("9").onclick = null;
document.getElementById("12").onclick = null;
document.getElementById("14").onclick = null;
document.getElementById("21").onclick = null;
document.getElementById("34").onclick = null;
document.onclick = null;
else
alert("You Must Find The Code To Use The Lock.");
I'm trying to add a secondary function after this, how would I go about that? Would I use "addEventListener" this time around? Or would there be another method?
– Equity
Mar 8 at 23:00
@Equity No you can usedocument.getElementById("button").onclick = newFunction;
to assign a new function, but you can also use addEventListener if you would rather do that.
– Nils Kähler
Mar 8 at 23:13
@Equity You can use whatever method you want. You just have to be consistent between the way you add the listener and remove it.
– Barmar
Mar 8 at 23:16
add a comment |
removeEventListener
can only remove listeners that were added with addEventListener
. If you use onclick
attributes, you need to assign to that property.
function useCodelock()
if (one == 1 && nine == 1 && five == 1 & seven == 1 && two == 1 && ninetwo == 1)
document.getElementById("main-view").style.backgroundImage = "url('images/gray.png')";
document.getElementById("1").onclick = null;
document.getElementById("6").onclick = null;
document.getElementById("9").onclick = null;
document.getElementById("12").onclick = null;
document.getElementById("14").onclick = null;
document.getElementById("21").onclick = null;
document.getElementById("34").onclick = null;
document.onclick = null;
else
alert("You Must Find The Code To Use The Lock.");
I'm trying to add a secondary function after this, how would I go about that? Would I use "addEventListener" this time around? Or would there be another method?
– Equity
Mar 8 at 23:00
@Equity No you can usedocument.getElementById("button").onclick = newFunction;
to assign a new function, but you can also use addEventListener if you would rather do that.
– Nils Kähler
Mar 8 at 23:13
@Equity You can use whatever method you want. You just have to be consistent between the way you add the listener and remove it.
– Barmar
Mar 8 at 23:16
add a comment |
removeEventListener
can only remove listeners that were added with addEventListener
. If you use onclick
attributes, you need to assign to that property.
function useCodelock()
if (one == 1 && nine == 1 && five == 1 & seven == 1 && two == 1 && ninetwo == 1)
document.getElementById("main-view").style.backgroundImage = "url('images/gray.png')";
document.getElementById("1").onclick = null;
document.getElementById("6").onclick = null;
document.getElementById("9").onclick = null;
document.getElementById("12").onclick = null;
document.getElementById("14").onclick = null;
document.getElementById("21").onclick = null;
document.getElementById("34").onclick = null;
document.onclick = null;
else
alert("You Must Find The Code To Use The Lock.");
removeEventListener
can only remove listeners that were added with addEventListener
. If you use onclick
attributes, you need to assign to that property.
function useCodelock()
if (one == 1 && nine == 1 && five == 1 & seven == 1 && two == 1 && ninetwo == 1)
document.getElementById("main-view").style.backgroundImage = "url('images/gray.png')";
document.getElementById("1").onclick = null;
document.getElementById("6").onclick = null;
document.getElementById("9").onclick = null;
document.getElementById("12").onclick = null;
document.getElementById("14").onclick = null;
document.getElementById("21").onclick = null;
document.getElementById("34").onclick = null;
document.onclick = null;
else
alert("You Must Find The Code To Use The Lock.");
answered Mar 8 at 22:50
BarmarBarmar
434k36260363
434k36260363
I'm trying to add a secondary function after this, how would I go about that? Would I use "addEventListener" this time around? Or would there be another method?
– Equity
Mar 8 at 23:00
@Equity No you can usedocument.getElementById("button").onclick = newFunction;
to assign a new function, but you can also use addEventListener if you would rather do that.
– Nils Kähler
Mar 8 at 23:13
@Equity You can use whatever method you want. You just have to be consistent between the way you add the listener and remove it.
– Barmar
Mar 8 at 23:16
add a comment |
I'm trying to add a secondary function after this, how would I go about that? Would I use "addEventListener" this time around? Or would there be another method?
– Equity
Mar 8 at 23:00
@Equity No you can usedocument.getElementById("button").onclick = newFunction;
to assign a new function, but you can also use addEventListener if you would rather do that.
– Nils Kähler
Mar 8 at 23:13
@Equity You can use whatever method you want. You just have to be consistent between the way you add the listener and remove it.
– Barmar
Mar 8 at 23:16
I'm trying to add a secondary function after this, how would I go about that? Would I use "addEventListener" this time around? Or would there be another method?
– Equity
Mar 8 at 23:00
I'm trying to add a secondary function after this, how would I go about that? Would I use "addEventListener" this time around? Or would there be another method?
– Equity
Mar 8 at 23:00
@Equity No you can use
document.getElementById("button").onclick = newFunction;
to assign a new function, but you can also use addEventListener if you would rather do that.– Nils Kähler
Mar 8 at 23:13
@Equity No you can use
document.getElementById("button").onclick = newFunction;
to assign a new function, but you can also use addEventListener if you would rather do that.– Nils Kähler
Mar 8 at 23:13
@Equity You can use whatever method you want. You just have to be consistent between the way you add the listener and remove it.
– Barmar
Mar 8 at 23:16
@Equity You can use whatever method you want. You just have to be consistent between the way you add the listener and remove it.
– Barmar
Mar 8 at 23:16
add a comment |
A call like removeListener("click", myFunc, true);
will only remove a listener that was added using addListener("click", myFunc, true);
. Here is an example:
var flag = true;
function toggle()
if (flag) document.getElementById("1").addEventListener("click", removeImg);
else document.getElementById("1").removeEventListener("click", removeImg);
flag = !flag;
function removeImg()
console.log("Clicked");
document.getElementById("toggle").addEventListener("click", toggle);
<button id="1">Test</button>
<button id="toggle">Toggle</button>
Here is a list of things you should check if it still doesn't work:
- Use the same function in the
addEventListener
as in theremoveListener
(() =>
is not the same as() =>
) - Use the same
useCapture
value (both need to betrue
or both need to befalse
) - Check that you are removing the listener from the correct element
- Check that the listener has only been added once to the element
add a comment |
A call like removeListener("click", myFunc, true);
will only remove a listener that was added using addListener("click", myFunc, true);
. Here is an example:
var flag = true;
function toggle()
if (flag) document.getElementById("1").addEventListener("click", removeImg);
else document.getElementById("1").removeEventListener("click", removeImg);
flag = !flag;
function removeImg()
console.log("Clicked");
document.getElementById("toggle").addEventListener("click", toggle);
<button id="1">Test</button>
<button id="toggle">Toggle</button>
Here is a list of things you should check if it still doesn't work:
- Use the same function in the
addEventListener
as in theremoveListener
(() =>
is not the same as() =>
) - Use the same
useCapture
value (both need to betrue
or both need to befalse
) - Check that you are removing the listener from the correct element
- Check that the listener has only been added once to the element
add a comment |
A call like removeListener("click", myFunc, true);
will only remove a listener that was added using addListener("click", myFunc, true);
. Here is an example:
var flag = true;
function toggle()
if (flag) document.getElementById("1").addEventListener("click", removeImg);
else document.getElementById("1").removeEventListener("click", removeImg);
flag = !flag;
function removeImg()
console.log("Clicked");
document.getElementById("toggle").addEventListener("click", toggle);
<button id="1">Test</button>
<button id="toggle">Toggle</button>
Here is a list of things you should check if it still doesn't work:
- Use the same function in the
addEventListener
as in theremoveListener
(() =>
is not the same as() =>
) - Use the same
useCapture
value (both need to betrue
or both need to befalse
) - Check that you are removing the listener from the correct element
- Check that the listener has only been added once to the element
A call like removeListener("click", myFunc, true);
will only remove a listener that was added using addListener("click", myFunc, true);
. Here is an example:
var flag = true;
function toggle()
if (flag) document.getElementById("1").addEventListener("click", removeImg);
else document.getElementById("1").removeEventListener("click", removeImg);
flag = !flag;
function removeImg()
console.log("Clicked");
document.getElementById("toggle").addEventListener("click", toggle);
<button id="1">Test</button>
<button id="toggle">Toggle</button>
Here is a list of things you should check if it still doesn't work:
- Use the same function in the
addEventListener
as in theremoveListener
(() =>
is not the same as() =>
) - Use the same
useCapture
value (both need to betrue
or both need to befalse
) - Check that you are removing the listener from the correct element
- Check that the listener has only been added once to the element
var flag = true;
function toggle()
if (flag) document.getElementById("1").addEventListener("click", removeImg);
else document.getElementById("1").removeEventListener("click", removeImg);
flag = !flag;
function removeImg()
console.log("Clicked");
document.getElementById("toggle").addEventListener("click", toggle);
<button id="1">Test</button>
<button id="toggle">Toggle</button>
var flag = true;
function toggle()
if (flag) document.getElementById("1").addEventListener("click", removeImg);
else document.getElementById("1").removeEventListener("click", removeImg);
flag = !flag;
function removeImg()
console.log("Clicked");
document.getElementById("toggle").addEventListener("click", toggle);
<button id="1">Test</button>
<button id="toggle">Toggle</button>
answered Mar 8 at 22:56
nick zoumnick zoum
2,60211540
2,60211540
add a comment |
add a comment |
You need to unbind the onclick function by setting it to null instead of using removeEventListener
.
function myFunction()
console.log('unbind me');
// if you want to unbind the function
document.getElementById("button").onclick = null;
// if you want to assign a new unction.
document.getElementById("button").onclick = newFunction;
function newFunction()
console.log("hello new");
<button id="button" onclick="myFunction()">
unbind me
</button>
You are trying to remove an eventlistener that is not present, because it is never set by addEventListener
. You have an onclick listener that is set on the DOM, so you need to unbind that function by setting it to null.
add a comment |
You need to unbind the onclick function by setting it to null instead of using removeEventListener
.
function myFunction()
console.log('unbind me');
// if you want to unbind the function
document.getElementById("button").onclick = null;
// if you want to assign a new unction.
document.getElementById("button").onclick = newFunction;
function newFunction()
console.log("hello new");
<button id="button" onclick="myFunction()">
unbind me
</button>
You are trying to remove an eventlistener that is not present, because it is never set by addEventListener
. You have an onclick listener that is set on the DOM, so you need to unbind that function by setting it to null.
add a comment |
You need to unbind the onclick function by setting it to null instead of using removeEventListener
.
function myFunction()
console.log('unbind me');
// if you want to unbind the function
document.getElementById("button").onclick = null;
// if you want to assign a new unction.
document.getElementById("button").onclick = newFunction;
function newFunction()
console.log("hello new");
<button id="button" onclick="myFunction()">
unbind me
</button>
You are trying to remove an eventlistener that is not present, because it is never set by addEventListener
. You have an onclick listener that is set on the DOM, so you need to unbind that function by setting it to null.
You need to unbind the onclick function by setting it to null instead of using removeEventListener
.
function myFunction()
console.log('unbind me');
// if you want to unbind the function
document.getElementById("button").onclick = null;
// if you want to assign a new unction.
document.getElementById("button").onclick = newFunction;
function newFunction()
console.log("hello new");
<button id="button" onclick="myFunction()">
unbind me
</button>
You are trying to remove an eventlistener that is not present, because it is never set by addEventListener
. You have an onclick listener that is set on the DOM, so you need to unbind that function by setting it to null.
function myFunction()
console.log('unbind me');
// if you want to unbind the function
document.getElementById("button").onclick = null;
// if you want to assign a new unction.
document.getElementById("button").onclick = newFunction;
function newFunction()
console.log("hello new");
<button id="button" onclick="myFunction()">
unbind me
</button>
function myFunction()
console.log('unbind me');
// if you want to unbind the function
document.getElementById("button").onclick = null;
// if you want to assign a new unction.
document.getElementById("button").onclick = newFunction;
function newFunction()
console.log("hello new");
<button id="button" onclick="myFunction()">
unbind me
</button>
edited Mar 8 at 23:12
answered Mar 8 at 22:50
Nils KählerNils Kähler
606516
606516
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072041%2fjavascript-removeeventlistener-not-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown