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

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript