All Posts by Email, Once a Week

Media Temple logo

CSS-Tricks is brought to you in part by Media Temple, the web hosting that we both use and recommend.

Check if Event was Triggered or Native

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

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


  1. CodeRenaissance
    Permalink to comment#

    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

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed