Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afoysal's avatar

Directives issue in vue.js

My Html is like below

enter image description here

I wrote below code

directives: {
            'click-outside': {
                bind: function(el, binding, vNode) {

               var div = document.getElementsByClassName("container");

                    div[0].onclick = function(e) {
                        console.log('hello');
                    }

                }    
            }
        }

I am getting below error

enter image description here

0 likes
1 reply
Borisu's avatar

So a couple of problems.

  1. 'bind' will only execute your code once. So your event listener will be basically useless. You need 'inserted'.
  2. Make sure that your container div is not "covered" by any other div, or your click event will never get fired.
  3. Remove the quotes from the directive name.
  4. There is a different syntax for directives when using a component and when defining them globally. Check the documenatation: https://vuejs.org/v2/guide/custom-directive.html

Please or to participate in this conversation.