Events
are things that happen in the system you are programming, which the system tells you about so your code can react to them.
Events are fired to notify code of "interesting changes" that may affect code execution.
const btn = document.querySelector("button"); function random(number) { return Math.floor(Math.random() * (number + 1)); } function setNewRgbColor() { const rndCol = `rgb(${random(255)} ${random(255)} ${random(255)})`; document.body.style.backgroundColor = rndCol; } // adding listener btn.addEventListener("click", setNewRgbColor); // removing listener btn.removeEventListener("click", setNewRgbColor);
Mutliple handlers
myElement.addEventListener("click", functionA); myElement.addEventListener("click", functionB);
EventTarget
EventTarget
interface is implemented by objects that can receive events and may have listeners for them.
Any target of events implement three methods associated with this interface:
addEventListener()
removeEventListener()
dispatchEvent()
addEventListener()
Sets up a function that will be called when a specified event is delivered to the target.
Common targets are:
Mouse Events
Keyboard Events
Form Events
Touch Events
Window Events
Clipboard Events
Drag & Drop Events