Is there a way to close a vue component by clicking outside of it?Detect click outside elementWhy don't self-closing script elements work?What is the most efficient way to deep clone an object in JavaScript?How do I detect a click outside an element?Trigger a button click with JavaScript on the Enter key in a text boxWhat is the best way to add options to a select from as a JS object with jQuery?(Built-in) way in JavaScript to check if a string is a valid numberWhat is the best way to detect a mobile device?Disable click outside of bootstrap modal area to close modalDetect click outside React componentCall a Vue.js component method from outside the component

In gnome-terminal only 2 out of 3 zoom keys work

Does jamais mean always or never in this context?

Do I have an "anti-research" personality?

"ne paelici suspectaretur" (Tacitus)

What's the polite way to say "I need to urinate"?

How can I place the product on a social media post better?

Do generators produce a fixed load?

Transfer over $10k

What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?

Any examples of headwear for races with animal ears?

Is it possible to measure lightning discharges as Nikola Tesla?

Subtleties of choosing the sequence of tenses in Russian

Help, my Death Star suffers from Kessler syndrome!

What does YCWCYODFTRFDTY mean?

Find the coordinate of two line segments that are perpendicular

How can I get precisely a certain cubic cm by changing the following factors?

What word means to make something obsolete?

Python "triplet" dictionary?

Are Boeing 737-800’s grounded?

Why was Germany not as successful as other Europeans in establishing overseas colonies?

Feels like I am getting dragged in office politics

TikZ how to make supply and demand arrows for nodes?

Why does Bran Stark feel that Jon Snow "needs to know" about his lineage?

How to creep the reader out with what seems like a normal person?



Is there a way to close a vue component by clicking outside of it?


Detect click outside elementWhy don't self-closing script elements work?What is the most efficient way to deep clone an object in JavaScript?How do I detect a click outside an element?Trigger a button click with JavaScript on the Enter key in a text boxWhat is the best way to add options to a select from as a JS object with jQuery?(Built-in) way in JavaScript to check if a string is a valid numberWhat is the best way to detect a mobile device?Disable click outside of bootstrap modal area to close modalDetect click outside React componentCall a Vue.js component method from outside the component






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am looking for a way to close a component when there it a click outisde of the element.



I tried an addEventListener.
This closes the component but after being closed it will not open again.



window.addEventListener('click', function(e)

if (document.getElementById('shopcartpreview').contains(e.target))
console.log("Clicked in Box");


else
console.log("Clicked outside Box");
$('#shopcartpreview').hide();

)


Is there a way to accomplish this?



<template>
<div id="shopcartpreview" v-if="carthover">
<div class="cartitem" v-for="item in cartitems">
<div class="cartitempic"><img class="productImg" width="80px" height="80px" v-bind:src="'assets/products/' + item.image"></div>
<div class="cartitemdetails">
<div class="cartitemname">item.name</div>
<div class="cartitemqty">1 X </div>
<div class="cartitemprice">€item.unit_price</div>
</div>
<div class="cartitemdelete">
<img src="assets/images/icon-bin.png" width="15px" height="15px">
</div>
</div>

<div class="carttotal">
<div class="carttotaltext">TOTAL:</div>
<div class="carttotalprice">€2,860.00</div>
</div>
<div class="cartcheckouttbn">PROCEED TO CHECKOUT</div>
<div class="viewcart">VIEW CART</div>



</div>
</template>
<script>
module.exports =
data: function ()
return
cartitems: 0,
carthover: false,

,
created()
EventBus.$on('addToCart', (payload) =>
this.cartitems = payload
),
EventBus.$on('mouseover', (carthover) =>
this.carthover = carthover
)


</script>









share|improve this question
























  • what is your code to show the component?

    – varit05
    Mar 22 at 19:23











  • I just updated my question

    – MK01111000
    Mar 22 at 19:27











  • How are you trying to show the component again in here?

    – varit05
    Mar 22 at 19:29






  • 1





    You can simply put a button and toggle the component with v-show directive

    – varit05
    Mar 22 at 19:39






  • 2





    This can help : click-outside directive

    – Sovalina
    Mar 22 at 19:42

















0















I am looking for a way to close a component when there it a click outisde of the element.



I tried an addEventListener.
This closes the component but after being closed it will not open again.



window.addEventListener('click', function(e)

if (document.getElementById('shopcartpreview').contains(e.target))
console.log("Clicked in Box");


else
console.log("Clicked outside Box");
$('#shopcartpreview').hide();

)


Is there a way to accomplish this?



<template>
<div id="shopcartpreview" v-if="carthover">
<div class="cartitem" v-for="item in cartitems">
<div class="cartitempic"><img class="productImg" width="80px" height="80px" v-bind:src="'assets/products/' + item.image"></div>
<div class="cartitemdetails">
<div class="cartitemname">item.name</div>
<div class="cartitemqty">1 X </div>
<div class="cartitemprice">€item.unit_price</div>
</div>
<div class="cartitemdelete">
<img src="assets/images/icon-bin.png" width="15px" height="15px">
</div>
</div>

<div class="carttotal">
<div class="carttotaltext">TOTAL:</div>
<div class="carttotalprice">€2,860.00</div>
</div>
<div class="cartcheckouttbn">PROCEED TO CHECKOUT</div>
<div class="viewcart">VIEW CART</div>



</div>
</template>
<script>
module.exports =
data: function ()
return
cartitems: 0,
carthover: false,

,
created()
EventBus.$on('addToCart', (payload) =>
this.cartitems = payload
),
EventBus.$on('mouseover', (carthover) =>
this.carthover = carthover
)


</script>









share|improve this question
























  • what is your code to show the component?

    – varit05
    Mar 22 at 19:23











  • I just updated my question

    – MK01111000
    Mar 22 at 19:27











  • How are you trying to show the component again in here?

    – varit05
    Mar 22 at 19:29






  • 1





    You can simply put a button and toggle the component with v-show directive

    – varit05
    Mar 22 at 19:39






  • 2





    This can help : click-outside directive

    – Sovalina
    Mar 22 at 19:42













0












0








0








I am looking for a way to close a component when there it a click outisde of the element.



I tried an addEventListener.
This closes the component but after being closed it will not open again.



window.addEventListener('click', function(e)

if (document.getElementById('shopcartpreview').contains(e.target))
console.log("Clicked in Box");


else
console.log("Clicked outside Box");
$('#shopcartpreview').hide();

)


Is there a way to accomplish this?



<template>
<div id="shopcartpreview" v-if="carthover">
<div class="cartitem" v-for="item in cartitems">
<div class="cartitempic"><img class="productImg" width="80px" height="80px" v-bind:src="'assets/products/' + item.image"></div>
<div class="cartitemdetails">
<div class="cartitemname">item.name</div>
<div class="cartitemqty">1 X </div>
<div class="cartitemprice">€item.unit_price</div>
</div>
<div class="cartitemdelete">
<img src="assets/images/icon-bin.png" width="15px" height="15px">
</div>
</div>

<div class="carttotal">
<div class="carttotaltext">TOTAL:</div>
<div class="carttotalprice">€2,860.00</div>
</div>
<div class="cartcheckouttbn">PROCEED TO CHECKOUT</div>
<div class="viewcart">VIEW CART</div>



</div>
</template>
<script>
module.exports =
data: function ()
return
cartitems: 0,
carthover: false,

,
created()
EventBus.$on('addToCart', (payload) =>
this.cartitems = payload
),
EventBus.$on('mouseover', (carthover) =>
this.carthover = carthover
)


</script>









share|improve this question
















I am looking for a way to close a component when there it a click outisde of the element.



I tried an addEventListener.
This closes the component but after being closed it will not open again.



window.addEventListener('click', function(e)

if (document.getElementById('shopcartpreview').contains(e.target))
console.log("Clicked in Box");


else
console.log("Clicked outside Box");
$('#shopcartpreview').hide();

)


Is there a way to accomplish this?



<template>
<div id="shopcartpreview" v-if="carthover">
<div class="cartitem" v-for="item in cartitems">
<div class="cartitempic"><img class="productImg" width="80px" height="80px" v-bind:src="'assets/products/' + item.image"></div>
<div class="cartitemdetails">
<div class="cartitemname">item.name</div>
<div class="cartitemqty">1 X </div>
<div class="cartitemprice">€item.unit_price</div>
</div>
<div class="cartitemdelete">
<img src="assets/images/icon-bin.png" width="15px" height="15px">
</div>
</div>

<div class="carttotal">
<div class="carttotaltext">TOTAL:</div>
<div class="carttotalprice">€2,860.00</div>
</div>
<div class="cartcheckouttbn">PROCEED TO CHECKOUT</div>
<div class="viewcart">VIEW CART</div>



</div>
</template>
<script>
module.exports =
data: function ()
return
cartitems: 0,
carthover: false,

,
created()
EventBus.$on('addToCart', (payload) =>
this.cartitems = payload
),
EventBus.$on('mouseover', (carthover) =>
this.carthover = carthover
)


</script>






javascript vue.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 19:26







MK01111000

















asked Mar 22 at 19:20









MK01111000MK01111000

947




947












  • what is your code to show the component?

    – varit05
    Mar 22 at 19:23











  • I just updated my question

    – MK01111000
    Mar 22 at 19:27











  • How are you trying to show the component again in here?

    – varit05
    Mar 22 at 19:29






  • 1





    You can simply put a button and toggle the component with v-show directive

    – varit05
    Mar 22 at 19:39






  • 2





    This can help : click-outside directive

    – Sovalina
    Mar 22 at 19:42

















  • what is your code to show the component?

    – varit05
    Mar 22 at 19:23











  • I just updated my question

    – MK01111000
    Mar 22 at 19:27











  • How are you trying to show the component again in here?

    – varit05
    Mar 22 at 19:29






  • 1





    You can simply put a button and toggle the component with v-show directive

    – varit05
    Mar 22 at 19:39






  • 2





    This can help : click-outside directive

    – Sovalina
    Mar 22 at 19:42
















what is your code to show the component?

– varit05
Mar 22 at 19:23





what is your code to show the component?

– varit05
Mar 22 at 19:23













I just updated my question

– MK01111000
Mar 22 at 19:27





I just updated my question

– MK01111000
Mar 22 at 19:27













How are you trying to show the component again in here?

– varit05
Mar 22 at 19:29





How are you trying to show the component again in here?

– varit05
Mar 22 at 19:29




1




1





You can simply put a button and toggle the component with v-show directive

– varit05
Mar 22 at 19:39





You can simply put a button and toggle the component with v-show directive

– varit05
Mar 22 at 19:39




2




2





This can help : click-outside directive

– Sovalina
Mar 22 at 19:42





This can help : click-outside directive

– Sovalina
Mar 22 at 19:42












1 Answer
1






active

oldest

votes


















1














Demo Fiddle : https://jsfiddle.net/bq8m4fhe/



Create a clickoutside directive ... Detect click outside element



 module.exports = 
data: function ()
return
cartitems: 0,
carthover: false,

,
directives:
clickoutside:
bind: function (el, binding, vnode)
el.clickOutsideEvent = function (event) ;
document.body.addEventListener('click', el.clickOutsideEvent)
document.body.addEventListener('touchstart', el.clickOutsideEvent)
,
unbind: function (el)
document.body.removeEventListener('click', el.clickOutsideEvent)
document.body.removeEventListener('touchstart', el.clickOutsideEvent)
,
stopProp(event) event.stopPropagation()

,
created()
EventBus.$on('addToCart', (payload) =>
this.cartitems = payload
),
EventBus.$on('mouseover', (carthover) =>
this.carthover = carthover
)




Use that directive like this.



<div id="shopcartpreview" v-if="carthover" v-clickoutside="SHOPPING_CART_HIDE_FUNCTION">





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306489%2fis-there-a-way-to-close-a-vue-component-by-clicking-outside-of-it%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Demo Fiddle : https://jsfiddle.net/bq8m4fhe/



    Create a clickoutside directive ... Detect click outside element



     module.exports = 
    data: function ()
    return
    cartitems: 0,
    carthover: false,

    ,
    directives:
    clickoutside:
    bind: function (el, binding, vnode)
    el.clickOutsideEvent = function (event) ;
    document.body.addEventListener('click', el.clickOutsideEvent)
    document.body.addEventListener('touchstart', el.clickOutsideEvent)
    ,
    unbind: function (el)
    document.body.removeEventListener('click', el.clickOutsideEvent)
    document.body.removeEventListener('touchstart', el.clickOutsideEvent)
    ,
    stopProp(event) event.stopPropagation()

    ,
    created()
    EventBus.$on('addToCart', (payload) =>
    this.cartitems = payload
    ),
    EventBus.$on('mouseover', (carthover) =>
    this.carthover = carthover
    )




    Use that directive like this.



    <div id="shopcartpreview" v-if="carthover" v-clickoutside="SHOPPING_CART_HIDE_FUNCTION">





    share|improve this answer





























      1














      Demo Fiddle : https://jsfiddle.net/bq8m4fhe/



      Create a clickoutside directive ... Detect click outside element



       module.exports = 
      data: function ()
      return
      cartitems: 0,
      carthover: false,

      ,
      directives:
      clickoutside:
      bind: function (el, binding, vnode)
      el.clickOutsideEvent = function (event) ;
      document.body.addEventListener('click', el.clickOutsideEvent)
      document.body.addEventListener('touchstart', el.clickOutsideEvent)
      ,
      unbind: function (el)
      document.body.removeEventListener('click', el.clickOutsideEvent)
      document.body.removeEventListener('touchstart', el.clickOutsideEvent)
      ,
      stopProp(event) event.stopPropagation()

      ,
      created()
      EventBus.$on('addToCart', (payload) =>
      this.cartitems = payload
      ),
      EventBus.$on('mouseover', (carthover) =>
      this.carthover = carthover
      )




      Use that directive like this.



      <div id="shopcartpreview" v-if="carthover" v-clickoutside="SHOPPING_CART_HIDE_FUNCTION">





      share|improve this answer



























        1












        1








        1







        Demo Fiddle : https://jsfiddle.net/bq8m4fhe/



        Create a clickoutside directive ... Detect click outside element



         module.exports = 
        data: function ()
        return
        cartitems: 0,
        carthover: false,

        ,
        directives:
        clickoutside:
        bind: function (el, binding, vnode)
        el.clickOutsideEvent = function (event) ;
        document.body.addEventListener('click', el.clickOutsideEvent)
        document.body.addEventListener('touchstart', el.clickOutsideEvent)
        ,
        unbind: function (el)
        document.body.removeEventListener('click', el.clickOutsideEvent)
        document.body.removeEventListener('touchstart', el.clickOutsideEvent)
        ,
        stopProp(event) event.stopPropagation()

        ,
        created()
        EventBus.$on('addToCart', (payload) =>
        this.cartitems = payload
        ),
        EventBus.$on('mouseover', (carthover) =>
        this.carthover = carthover
        )




        Use that directive like this.



        <div id="shopcartpreview" v-if="carthover" v-clickoutside="SHOPPING_CART_HIDE_FUNCTION">





        share|improve this answer















        Demo Fiddle : https://jsfiddle.net/bq8m4fhe/



        Create a clickoutside directive ... Detect click outside element



         module.exports = 
        data: function ()
        return
        cartitems: 0,
        carthover: false,

        ,
        directives:
        clickoutside:
        bind: function (el, binding, vnode)
        el.clickOutsideEvent = function (event) ;
        document.body.addEventListener('click', el.clickOutsideEvent)
        document.body.addEventListener('touchstart', el.clickOutsideEvent)
        ,
        unbind: function (el)
        document.body.removeEventListener('click', el.clickOutsideEvent)
        document.body.removeEventListener('touchstart', el.clickOutsideEvent)
        ,
        stopProp(event) event.stopPropagation()

        ,
        created()
        EventBus.$on('addToCart', (payload) =>
        this.cartitems = payload
        ),
        EventBus.$on('mouseover', (carthover) =>
        this.carthover = carthover
        )




        Use that directive like this.



        <div id="shopcartpreview" v-if="carthover" v-clickoutside="SHOPPING_CART_HIDE_FUNCTION">






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 22 at 20:24

























        answered Mar 22 at 19:55









        dagaltidagalti

        73237




        73237





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306489%2fis-there-a-way-to-close-a-vue-component-by-clicking-outside-of-it%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현