onBind method

Selection onBind(
  1. String typename,
  2. OnListener? listener, [
  3. JSAny? options
])

Adds or removes a listener to each selected element for the specified event typenames.

d4.selectAll("p".u31).onBind(
    "click", (EventTarget? thisArg, Event event, JSAny? d) => print(event));

The typenames is a string event type, such as click, mouseover, or submit; any DOM event type supported by your browser may be used. The type may be optionally followed by a period (.) and a name; the optional name allows multiple callbacks to be registered to receive events of the same type, such as click.foo and click.bar. To specify multiple typenames, separate typenames with spaces, such as input change or click.foo click.bar.

When a specified event is dispatched on a selected element, the specified listener will be evaluated for the element, being passed the current event (event) and the current datum (d), with thisArg as the current DOM element (event.currentTarget). Listeners always see the latest datum for their element. Note: while you can use event.pageX and event.pageY directly, it is often convenient to transform the event position to the local coordinate system of the element that received the event using d4.pointer.

If an event listener was previously registered for the same typename on a selected element, the old listener is removed before the new listener is added. To remove a listener, pass null as the listener. To remove all listeners for a given name, pass null as the listener and .foo as the typename, where foo is the name; to remove all listeners with no name, specify . as the typename.

An optional options object may specify characteristics about the event listener, such as whether it is capturing or passive; see element.addEventListener.

Implementation

Selection onBind(String typename, OnListener? listener, [JSAny? options]) {
  var typenames = parseTypenames(typename);

  final on = listener != null ? onAdd : onRemove;
  for (final t in typenames) {
    each(on(t.jsify() as JSObject, listener, options));
  }
  return this;
}