Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  48] [ 5]  / answers: 1 / hits: 72632  / 13 Years ago, thu, july 14, 2011, 12:00:00

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

More From » jquery

 Answers
1

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();
});

[#91180] Wednesday, July 13, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarrodfletchers

Total Points: 75
Total Questions: 94
Total Answers: 95

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
jarrodfletchers questions
Sat, Sep 25, 21, 00:00, 3 Years ago
Mon, Jan 21, 19, 00:00, 5 Years ago
Sun, Dec 16, 18, 00:00, 6 Years ago
Sun, Nov 4, 18, 00:00, 6 Years ago
;