Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if event is triggered by a human

I have a handler attached to an event and I would like it to execute only if it is triggered by a human, and not by a trigger() method. How do I tell the difference?

For example,

$('.checkbox').change(function(e){
  if (e.isHuman())
  {
    alert ('human');
  }
});

$('.checkbox').trigger('change'); //doesn't alert
like image 563
Dziamid Avatar asked Oct 17 '22 07:10

Dziamid


People also ask

How can you tell if a event is triggered?

Open Google Chrome and press F12 to open Dev Tools. Go to Event Listener Breakpoints, on the right: Click on the events and interact with the target element.

How do you check event is triggered or not in jQuery?

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

What is the difference between event and trigger?

A Trigger is a set of conditions that cause an interaction to run. An event is a user- or administrator-initiated action that an administrator defines that must be met before an interaction is created. Triggers listen for events, and once that event occurs, can initiate an interaction defined within Journey Builder.

Is event a trigger?

An event trigger is an association between a predefined event and the script that is to be executed when that event occurs. These scripts fall into two categories based on when they occur and if they modify entries in the database.


1 Answers

You can check e.originalEvent: if it's defined the click is human:

Look at the fiddle http://jsfiddle.net/Uf8Wv/

$('.checkbox').change(function(e){
  if (e.originalEvent !== undefined)
  {
    alert ('human');
  }
});

my example in the fiddle:

<input type='checkbox' id='try' >try
<button id='click'>Click</button>

$("#try").click(function(event) {
    if (event.originalEvent === undefined) {
        alert('not human')
    } else {
        alert(' human');
    }


});

$('#click').click(function(event) {
    $("#try").click();
});
like image 82
Nicola Peluchetti Avatar answered Nov 15 '22 16:11

Nicola Peluchetti