最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Manually trigger event listener from Chrome console - Stack Overflow

programmeradmin5浏览0评论

I don't know much about Javascript but I though the .click mand was the way to simulate a click on an item, let's say with something like

document.getElementById('id').click

I recently found a webpage where the buttons .click mand did not triggered anything though my actual real clicks do.

I dug a bit and found that there are no event on the onclick of those buttons though they seem to have some eventlistener attached.

Hence I am looking to the proper mand out of my Chrome console to trigger the event without actually clicking. Anyone can help on this?

Regards

I don't know much about Javascript but I though the .click mand was the way to simulate a click on an item, let's say with something like

document.getElementById('id').click

I recently found a webpage where the buttons .click mand did not triggered anything though my actual real clicks do.

I dug a bit and found that there are no event on the onclick of those buttons though they seem to have some eventlistener attached.

Hence I am looking to the proper mand out of my Chrome console to trigger the event without actually clicking. Anyone can help on this?

Regards

Share Improve this question edited Jul 16, 2019 at 17:35 Tobias Tengler 7,4744 gold badges25 silver badges34 bronze badges asked Jun 24, 2018 at 19:58 samuel guedonsamuel guedon 6261 gold badge9 silver badges23 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Ok that was a very stupid question from me thanks to my lack of knowledge of javascript (and tiredness maybe regarding how easy the answer is).

So I just lacked a few parenthesis in my click mand...

(EDIT: fixed typo in method)

document.getElementById('id').click()

And now it's fine... sorry for bothering you and thanks to @Kasabucki Alexandr who indirectly showed me the correct answer

You can just type in console something like this: document.getElementById('notify-container');

let a = document.getElementById('notify-container');

a.addEventListener('click', () => console.log('asdfsd'));

document.getElementById('notify-container').click()
<div id="notify-container"></div>

Do you think about addEventListener?

发布评论

评论列表(0)

  1. 暂无评论