-4

I have a clickable div and I would like to disable it in some cases.

    <div id="div" routerLink="/url" class="module-dashboard"></div>

I have tried:

var div= <HTMLDivElement>document.getElementById("div");
div.disabled = true;

But "disabled" is not a property of HTMLDivElement

What I need is to avoid the routerLink.

Is it possible to do it?

bellotas
  • 2,349
  • 8
  • 29
  • 60

2 Answers2

1

The removeEventListener() method removes an event handler that has been attached with the addEventListener() method.

var _el = document.getElementById("div");
_el.removeEventListener("click", eventHandler);

Here eventHandler is the function you attached on click using addEventListener. Anonymous function like "element.removeEventListener("event", function(){ myScript }) will not work.

void
  • 36,090
  • 8
  • 62
  • 107
  • 1
    This applies ONLY if he attached the event handler with `addEventListener`. This wont work if he used `onclick` in the element. – Cristian S. Feb 16 '18 at 08:24
0

Try this. Click will work only once.

var e = document.getElementById("button");
e.onclick = function() {
  this.onclick = null;
  console.log("disabled!");
};
<div id="button">Clickable DIV</div>
CodeLover
  • 571
  • 2
  • 11