Danny Tuppeny
Danny Tuppeny

Reputation: 42333

How do I create an`Event` instance with the `target` property set?

When I subscribe to the onChange event of an InputElement, the event type is Event and has the target property set:

new InputElement()
  ..onChange.listen((e) {
    print(e.runtimeType);
    print(e.target);
  });

How do I copy this behavior (for my own custom input box) and create events with the target property set?

None of the Event constructors allow you to pass a target, and the target property is get-only.

I tried finding the source for InputElement to see how it worked; but was unable to locate it in the Dart repo :(

Upvotes: 1

Views: 57

Answers (2)

Danny Tuppeny
Danny Tuppeny

Reputation: 42333

You do this by calling dispatchEvent on the element, passing the Event:

var e = new Event.eventType('Event', 'change', canBubble: true, cancelable: false);
dispatchEvent(e);

Upvotes: 0

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657308

I think you should use a CustomEvent instead.

dispatchEvent(new CustomEvent('nameOfEvent'));

Just call dispatchEvent from the element you want to have set as target

  final someDiv = dom.querySelector('#some');
  someDiv.dispatchEvent(new dom.CustomEvent('xxx-yyy'));

In this question it is shown how to do it in Polymer elements How do I fire a custom event from Polymer Dart?

Upvotes: 1

Related Questions