3

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.

1 Answer 1

1
var chngEvt = document.createEvent("MouseEvents");
chngEvt.initMouseEvent('click', true, true);
e.target.dispatchEvent(chngEvt);
Sign up to request clarification or add additional context in comments.

3 Comments

You need to either add feature detection for support for createEvent and dispatchEvent, or warn the OP that not all browsers support those methods. Also useful to link to the current W3C standard and to an appropriate MDN article. And the last line should probably be this.dispatchEvent(…)
Guys, thanks for your answers. My target object does not implement dispatchEvent, because it is not DOM element, it is just custom object. Any tips of how to start with?
Hi David, the event framework is specific and reserved to the appropriate dom object. There's even an event constructor depending on the object category. If you wish to raise events from custom objects you will have to build your own custom framework. rgds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.