0

I'm trying to remove the click event listener of some cells in a table but I can't get it to work.

function DofmCalCheck() {

  const checkboxDofMElement = document.querySelectorAll('.checkboxDofM');
  var cells = document.querySelectorAll('#Dofm_Table td');

  cells.forEach(f => f.addEventListener('click', event => {
    document.querySelector("#" + f.getAttribute("data-val")).checked = !document.querySelector("#" + f.getAttribute("data-val")).checked;
  }));
}
DofmCalCheck();

function MonthDofmBlocks() {

  $("#Month_Table td").click(function(event) {
    var NoD = parseInt($(this).attr("data-test"));
    var Dofm = $("#Dofm_Table td");

    for (var i = 0; i < Dofm.length; i++) {

      var DofmVal = parseInt(Dofm[i].getAttribute("value")) + 1;

      if (DofmVal > NoD) {
        Dofm[i].classList.toggle('blocked');
        Dofm[i].removeEventListener("click", DofmCalCheck, false);
        console.log(Dofm[i]);
      }
    }
  });
}
MonthDofmBlocks();

Where I've added the removeEventListener method, I've set it to true and false but it still doesn't work.

31piy
  • 23,323
  • 6
  • 47
  • 67
MO MI
  • 85
  • 6
  • 2
    The DofmCalCheck function is not an EventListener, as far as I can see. You need to remove using a reference to the actual callback function which is invoked when the event occurs, but since you made anonymous methods for them, that might be tricky. See https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener#Matching_event_listeners_for_removal#Matching_event_listeners_for_removal for examples of how you're meant to do it. – ADyson Aug 03 '18 at 15:05
  • Oh I see! How can I cancel the click event I've set in DofmCalCheck? – MO MI Aug 03 '18 at 15:09
  • 1
    You may need a named and defined function to remove – LegenJerry Aug 03 '18 at 15:10
  • 1
    Possible duplicate of [removeEventListener on anonymous functions in JavaScript](https://stackoverflow.com/questions/4950115/removeeventlistener-on-anonymous-functions-in-javascript) – ADyson Aug 03 '18 at 15:23

0 Answers0