SnakeDoc SnakeDoc - 5 months ago 34
HTML Question

Determine if onclick was invoked via javascript

I have an input element which uses the

event to invoke some javascript function, like so:

<input type="checkbox" name="blah" id="blah" onclick="doSomeFunction();"/>

My issue is this
event is also invoked by some vendor code elsewhere, like:


I cannot modify the code that executes the
event of this element unfortunately, but can modify the

Is it possible to determine if the
event was invoked via javascript, or was an actual click?

Answer Source

If you can pass in the event object you can check for some differences in properties like clientX or clientY and isTrusted.

Note I've never done this before but tested it in Firefox and Chrome

function doSomeFunction(event) {
  console.log(event.isTrusted ? 'Manually clicked' : 'Clicked programatically');
  console.log('ClientX=', event.clientX)

$('button').click(function() {
<script src=""></script>

<input type="checkbox" name="blah" id="blah" onclick="doSomeFunction(event);" />

Trigger a click

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download