1

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 :(

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275

2 Answers2

1

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?

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

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);
Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275