3

I don't know much about Javascript but I though the .click command 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 command 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 command out of my Chrome console to trigger the event without actually clicking. Anyone can help on this?

Regards

Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
samuel guedon
  • 575
  • 1
  • 7
  • 21

3 Answers3

3

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 command...

(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

Community
  • 1
  • 1
samuel guedon
  • 575
  • 1
  • 7
  • 21
0

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>
-1

Do you think about addEventListener?

arczinosek
  • 139
  • 5
  • I understand that addEventListener is to add a listener (thank you captain obvious) not to trigger one. My aim is to trigger what has already been built from the person that made the page, i.e. reach the same result than when I actually click on the button but through the console – samuel guedon Jun 24 '18 at 20:10
  • did you think about [getEventListeners(node)](https://stackoverflow.com/a/15666321/9986020)? – arczinosek Jun 24 '18 at 20:16
  • thanks arczinosek, though my question was a bit stupid, see my answer – samuel guedon Jun 24 '18 at 20:19