{"id":9407,"date":"2024-01-25T04:42:33","date_gmt":"2024-01-25T04:42:33","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9407"},"modified":"2024-01-29T09:15:58","modified_gmt":"2024-01-29T09:15:58","slug":"detect-click-outside-element","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/vue\/detect-click-outside-element","title":{"rendered":"Detect Click Outside Element"},"content":{"rendered":"<p>To detect clicks outside of a specific element in Vue.js, you can create a custom directive or use a method that utilizes event listeners. Below are two approaches you can use:<\/p>\n<h3>Using Event Listeners<\/h3>\n<p>You can add an event listener to the document or the root element (<strong>window or document.body<\/strong>) and check if the click event occurred outside of a particular element.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">&lt;template&gt;\r\n &lt;div ref=\"elementToDetectOutsideClick\"&gt;\r\n   &lt;!-- Your content --&gt;\r\n &lt;\/div&gt;\r\n&lt;\/template&gt;\r\n\r\n&lt;script&gt;\r\nexport default {\r\n mounted() {\r\n   document.addEventListener('click', this.handleClickOutside);\r\n },\r\n beforeUnmount() {\r\n   document.removeEventListener('click', this.handleClickOutside);\r\n },\r\n methods: {\r\n   handleClickOutside(event) {\r\n     if (!this.$refs.elementToDetectOutsideClick.contains(event.target)) {\r\n       \/\/ Click occurred outside the element\r\n       \/\/ Your logic here\r\n     }\r\n   },\r\n },\r\n};\r\n&lt;\/script&gt;\r\n<\/pre>\n<h3>In this approach:<\/h3>\n<ul>\n<li>The <b>ref=&#8221;elementToDetectOutsideClick&#8221;<\/b> attribute is assigned to the element you want to track clicks outside of.<\/li>\n<li>The <b>handleClickOutside<\/b> method is triggered on every click, checking if the clicked element is outside the element with the given ref. If so, you can execute your logic.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>To detect clicks outside of a specific element in Vue.js, you can create a custom directive or use a method that utilizes event listeners. Below are two approaches you can use: Using Event Listeners You can add an event listener to the document or the root element (window or document.body) and check if the click [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9408,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-9407","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vue"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9407"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9407"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9407\/revisions"}],"predecessor-version":[{"id":9410,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9407\/revisions\/9410"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9408"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}