Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to differentiate between a button click triggered by a mouse click and a click triggered by the user's javascript?

Given a button reference, you can use javascript on your console to .click() it.

Is there a way for my website to differentiate between a click triggered by a mouse and a click triggered by the user’s javascript? Or are these effectively equivalent from the browser’s point of view?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use Event.isTrusted

Yes, you can use the event.isTrusted property. Will be true when the user clicks as shown in this code snippet.

mybutton.addEventListener("click", function(e) {
    console.log( "isTrusted = " + e.isTrusted );
});

test.addEventListener("click", function(e) {

  mybutton.click();

});
<button id="mybutton">My Button</button>

<button id="test">Test</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading