Kasper
Kasper

Reputation: 13602

Trigger a click on a HTML element (in dart)

I'm trying to trigger a click on html element (in dart).

In other words, how can I execute the function that is normally executed when the element is clicked ?

Here is an example:

import 'dart:html';
import 'dart:math';

main() {
  querySelector("#first").onClick.listen((e) {
    r() => new Random().nextInt(256);
    querySelector("#first").style.color =
      "rgb(${r()},${r()},${r()})";
  });

  querySelector("#second").onClick.listen((e) {
    // pretend the first paragraph is clicked
  });  
}

see also: https://dartpad.dartlang.org/456f1ec945536caf310c

Upvotes: 1

Views: 996

Answers (1)

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

Reputation: 657476

This should work to produce a mouse event

querySelector("#second").dispatchEvent(new MouseEvent(...))

Upvotes: 5

Related Questions