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?













2















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>









share|improve this question


























    2















    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>









    share|improve this question
























      2












      2








      2








      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>









      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 22:46









      EquityEquity

      132




      132






















          3 Answers
          3






          active

          oldest

          votes


















          2














          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.");







          share|improve this answer























          • 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 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


















          0














          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 the removeListener (() => is not the same as () => )

          • Use the same useCapture value (both need to be true or both need to be false)

          • Check that you are removing the listener from the correct element

          • Check that the listener has only been added once to the element





          share|improve this answer






























            0














            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.






            share|improve this answer

























              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
              );



              );













              draft saved

              draft discarded


















              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









              2














              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.");







              share|improve this answer























              • 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 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















              2














              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.");







              share|improve this answer























              • 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 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













              2












              2








              2







              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.");







              share|improve this answer













              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.");








              share|improve this answer












              share|improve this answer



              share|improve this answer










              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 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

















              • 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 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













              0














              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 the removeListener (() => is not the same as () => )

              • Use the same useCapture value (both need to be true or both need to be false)

              • Check that you are removing the listener from the correct element

              • Check that the listener has only been added once to the element





              share|improve this answer



























                0














                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 the removeListener (() => is not the same as () => )

                • Use the same useCapture value (both need to be true or both need to be false)

                • Check that you are removing the listener from the correct element

                • Check that the listener has only been added once to the element





                share|improve this answer

























                  0












                  0








                  0







                  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 the removeListener (() => is not the same as () => )

                  • Use the same useCapture value (both need to be true or both need to be false)

                  • Check that you are removing the listener from the correct element

                  • Check that the listener has only been added once to the element





                  share|improve this answer













                  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 the removeListener (() => is not the same as () => )

                  • Use the same useCapture value (both need to be true or both need to be false)

                  • 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>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 22:56









                  nick zoumnick zoum

                  2,60211540




                  2,60211540





















                      0














                      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.






                      share|improve this answer





























                        0














                        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.






                        share|improve this answer



























                          0












                          0








                          0







                          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.






                          share|improve this answer















                          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>






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 8 at 23:12

























                          answered Mar 8 at 22:50









                          Nils KählerNils Kähler

                          606516




                          606516



























                              draft saved

                              draft discarded
















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                              2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                              Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme