Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 992
sids Avatar asked Dec 04 '25 06:12

sids


1 Answers

var chngEvt = document.createEvent("MouseEvents");
chngEvt.initMouseEvent('click', true, true);
e.target.dispatchEvent(chngEvt);
like image 176
Sebas Avatar answered Dec 05 '25 22:12

Sebas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!