I'm using Popper.js to show a popup elment having the class .js-share-cf-popover
when clicking elements with class .js-share-cf-btn
.
But I want the popup to close only when I click outside of it. Here my actual code that show the popup:
var reference = $('.js-share-cf-btn');
var popover = $('.js-share-cf-popover');
popover.hide();
$(document).on('click', reference, function(e) {
e.preventDefault();
popover.show();
var popper = new Popper(reference, popover, {
placement: 'top',
});
});
I found something here but I can't get it works
Here My jsfiddle
Something like this should do the trick (by checking the target when you are clicking somewhere):
https://jsfiddle.net/e8aL9tje/
For those using React, I created a gist of an HOC that you can attach to any component to close it when clicked outside:
https://gist.github.com/elie222/850bc4adede99650508aba2090cd5da1
You can achieve this , by removing event delegation and check the target on event click by using the .is() , ( compare e.target if it equals to reference btn otherwise hide popup )
See fiddle
Added snippet as your code :
also made change in the
Popper
indtance you should pass the current clickjs-share-cf-btn
so the$(e.target)
element