Grow your CSS skills. Land your dream job.

Last updated on:

Check if Event was Triggered or Native

$('button').click(function(event, wasTriggered) {
    if (wasTriggered) {
        alert('triggered in code');
    } else {
        alert('triggered by mouse');

$('button').trigger('click', true);


  1. It’s probably good to note that if someone codes $(‘button’).click() it displays “triggered by mouse”.

  2. Ben
    Permalink to comment#

    It doesn’t look like jQuery’s newer .on() supports this technique. The following works by simply leveraging the event object:

    $('button').on('click', function (event) {
        if (e.originalEvent === undefined) alert ('triggered by code');
        else alert ('triggered by mouse');
    • Ben
      Permalink to comment#

      Sorry – mistyped

      ... if (event.originalEvent === undefined) ...
  3. nicolas
    Permalink to comment#

    hey Ben,
    I think your code is better:)

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".