How to set the EventTarget of a Event.
var myObj = {foo: 'bar'}; var event = new Event('eventName'); event.target = myObj;
Will result in the following error:
Uncaught TypeError: Cannot set property target of #<Event> which has only a getter
Edit:
I want to write something like this, where my class will be the target
:
class MyClass { constructor() { this.listeners = {}; } addEventListener(type, callback) { if (!(type in this.listeners)) { this.listeners[type] = []; } this.listeners[type].push(callback); } removeEventListener() { if (!(type in this.listeners)) { return; } var stack = this.listeners[type]; for (var i = 0, l = stack.length; i < l; i++) { if (stack[i] === callback) { stack.splice(i, 1); return this.removeEventListener(type, callback); } } } dispatchEvent(event) { if (!(event.type in this.listeners)) { return; } var stack = this.listeners[event.type]; event.target = this; for (var i = 0, l = stack.length; i < l; i++) { stack[i].call(this, event); } } } let myInstance = new MyClass(); let event = new Event('eventName'); myInstance.dispatchEvent();
More info: https://developer.mozilla.org/en/docs/Web/API/EventTarget
You can use event.target.id in event handler to get id of element that fired an event.
you can use event. target. parent() function.
The EventTarget interface is implemented by objects that can receive events and may have listeners for them. In other words, any target of events implements the three methods associated with this interface.
Okay - I just had a brainwave and so here's another answer for you that's very naughty and might just work. (worked for me ;)
Instead of doing event.target = myObj
, you could try:
Object.defineProperty(event, 'target', {writable: false, value: myObj});
I may be wrong here, but I think what happens is that the new target
property hides the original one. It doesn't work the same way as the browser internal event dispatching, and the browser event handling code will almost certainly ignore it, but it might just be enough to fool javascript code that relies on Event.target
.
Also, be careful to test it in different browsers, as some browsers might define Event.target
as a non-configurable property.
Regarding your edit, it markedly changes the question. The answer now is: Don't use CustomEvent
, you're trying to use it for something other than its intended purpose. Just use your own object, as you never dispatch the event to a DOM element.
It's set automatically when you dispatch the event:
targetElement.dispatchEvent(event);
If you want to include custom data other than the element with the event, simply add it as a property:
event.data = myObj;
Example:
"use strict"; var div = document.getElementById("target"); div.addEventListener( "eventName", function(e) { console.log("e.target:", e.target); console.log("e.data:", e.data); }, false ); var myObj = {foo: "bar"}; var event = new Event("eventName"); event.data = myObj; div.dispatchEvent(event);
<div id="target"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With