The addEventListener of EventTarget for JS appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
target.addEventListener(type, callback [, options]) The callback that will be invoked when the event is dispatched.
Allows setting the capture, once, passive, and signal attributes.
<!doctype html>
<html>
<body>
<output></output>
<script>
function callback(myparameter)
{
document.querySelector("output").innerHTML = myparameter + "<br>" + myparameter.target + "<br>" + myparameter.currentTarget.myargument;
}
const type = "mytype";
const event = new Event(type);
const target = new EventTarget();
target.addEventListener(type, callback);
target.myargument = "myargument";
target.dispatchEvent(event);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<button>button</button>
<output></output>
<script>
function callback(myparameter)
{
document.querySelector("output").innerHTML = myparameter + "<br>" + myparameter.target;
}
const type = "mouseover";
const target = document.querySelector("button");
target.addEventListener(type, callback);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<button>button</button>
<output></output>
<script>
function callback(myparameter)
{
document.querySelector("output").innerHTML = myparameter + "<br>" + myparameter.target + "<br>" + myparameter.currentTarget.myargument;
}
const type = "mouseover";
const target = document.querySelector("button");
target.addEventListener(type, callback);
target.myargument = "myargument";
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<div>
<button>button</button>
</div>
<output></output>
<script>
function callback(myparameter)
{
document.querySelector("output").innerHTML += myparameter.currentTarget.tagName + "<br>";
}
const type = "mouseover";
const options = false;
document.querySelector("div").addEventListener(type, callback, options);
document.querySelector("button").addEventListener(type, callback, options);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<div>
<button>button</button>
</div>
<output></output>
<script>
function callback(myparameter)
{
document.querySelector("output").innerHTML += myparameter.currentTarget.tagName + "<br>";
}
const type = "mouseover";
const options = true;
document.querySelector("div").addEventListener(type, callback, options);
document.querySelector("button").addEventListener(type, callback, options);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<div>
<button>button</button>
</div>
<output></output>
<script>
function callback(myparameter)
{
document.querySelector("output").innerHTML += myparameter.currentTarget.tagName + "<br>";
}
const type = "mouseover";
const options =
{
capture: false
};
document.querySelector("div").addEventListener(type, callback, options);
document.querySelector("button").addEventListener(type, callback, options);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<div>
<button>button</button>
</div>
<output></output>
<script>
function callback(myparameter)
{
document.querySelector("output").innerHTML += myparameter.currentTarget.tagName + "<br>";
}
const type = "mouseover";
const options =
{
capture: true
};
document.querySelector("div").addEventListener(type, callback, options);
document.querySelector("button").addEventListener(type, callback, options);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<button>button</button>
<output></output>
<script>
function callback(myparameter)
{
document.querySelector("output").innerHTML += "do something ";
}
const type = "mouseover";
const options =
{
once: false
};
document.querySelector("button").addEventListener(type, callback, options);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<button>button</button>
<output></output>
<script>
function callback(myparameter)
{
document.querySelector("output").innerHTML += "do something ";
}
const type = "mouseover";
const options =
{
once: true
};
document.querySelector("button").addEventListener(type, callback, options);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<input placeholder="prevent lowercase">
<script>
function callback(myparameter)
{
if((97 <= myparameter.charCode) && (myparameter.charCode <= 122))
{
myparameter.preventDefault();
}
}
const type = "keypress";
const options =
{
passive: false
};
document.querySelector("input").addEventListener(type, callback, options);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<input placeholder="prevent lowercase">
<p>preventDefault does not work when passive is set to true</p>
<script>
function callback(myparameter)
{
if((97 <= myparameter.charCode) && (myparameter.charCode <= 122))
{
myparameter.preventDefault();
}
}
const type = "keypress";
const options =
{
passive: true
};
document.querySelector("input").addEventListener(type, callback, options);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<button>button</button>
<output></output>
<script>
function callback(myparameter)
{
const element = document.querySelector("output");
if(element.innerHTML != "do something")
{
element.innerHTML = "do something";
}
else
{
element.innerHTML = "do something else";
controller.abort();
}
}
const controller = new AbortController();
const type = "mouseover";
const options =
{
signal: controller.signal
};
document.querySelector("button").addEventListener(type, callback, options);
</script>
</body>
</html>