0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

javascriptからクリックイベントを発生させる

Posted at

結論

 イベントを発生させるのは一般に、const e = new Event(~);などと記述できます。
 しかし、"click"イベント自体は(MouseEventを継承した)PointerEvent型なので、正しく扱うにはconst e = new PointerEvent("click");のような書き方となります1

 ターゲットのhtml要素 targetElement に対して、例えば

targetElement.dispatchEvent(new PointerEvent("click"));

とすることで、クリックされたものとしてイベントが送り込まれます2

  1. Element: click イベント - Web API | MDN
    https://developer.mozilla.org/ja/docs/Web/API/Element/click_event#%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E5%9E%8B

  2. EventTarget: dispatchEvent() メソッド - Web API | MDN
    https://developer.mozilla.org/ja/docs/Web/API/EventTarget/dispatchEvent

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?