25Oct

Check if we have a real user click or a code trigger click – jQuery

Sometimes you want an action to happen only when the user clicks. For instance, I had a rollover effect going trigger the same action as a click on filter button. However, if the button is clicked I wanted to avoid rollover to change the selected filter to keep the selected filter active. So if the user clicks, I run the trigger, if not I don’t run it.

How to check the difference between a user click and a programmatic trigger click?

With jQuery, you can use the originalEvent. Here is a bit of code on one way to use it:

$(".btn").on('click', function(evt) {
	if (evt.originalEvent === undefined) {
		// code triggered click
	} else {
		// real click from user
	}
});
Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments