David
David

Reputation: 560

How to modify target property of Event object in JavaScript?

I'm executing async code, so I wanted to trigger an event once I finished. I created Event object, and I want to pass my current "this" object as a target property of the event. However, this property do not change.

var e = new Event("success");
e.target = myTargetObject;
console.log(e.target); // return 'null' for any myTargetObject.

Upvotes: 3

Views: 2867

Answers (1)

Sebas
Sebas

Reputation: 21532

var chngEvt = document.createEvent("MouseEvents");
chngEvt.initMouseEvent('click', true, true);
e.target.dispatchEvent(chngEvt);

Upvotes: 1

Related Questions