How to detect if a click event was triggered by the user or programmatically

I often set up click handlers, then trigger it programmatically to set its default state (say an option click handler, where the first option is selected by default). This is a good pattern because you don’t end up with maintaining state via template and JS separately.

It’s also very common to want the programmatic click to happen very quietly, for example without scrolling the user down whereas a user click _should_ scroll the user down.

You simply need to check for the value of `event.originalEvent`.

if ev.originalEvent
console.log "This is a user event!"

Cool.

Leave a Comment