Skip to content
Advertisement

Detect click outside element

How can I detect a click outside my element? I’m using Vue.js so it’s gonna be outside my templates element. I know how to do it in Vanilla JS, but I’m not sure if there’s a more proper way to do it, when I’m using Vue.js?

This is the solution for Vanilla JS: Javascript Detect Click event outside of div

I guess I can use a better way to access the element?

Advertisement

Answer

Keep in attention that this solution only works with Vue 1.

Can be solved nicely by setting up a custom directive once:

JavaScript

Usage:

JavaScript

In the component:

JavaScript

Working Demo on JSFiddle with additional info about caveats:

https://jsfiddle.net/Linusborg/yzm8t8jq/

Advertisement