JSFiddle - React, Tailwind, and code Playground

by Wallace Maxters

HTML

<div id="click-out">
   SE clicar aqui, não fecha.
</div>

<div>Se clicar aqui (que é fora) fecha</div>

CSS

#click-out
{
    background-color:blue;
    padding:20px;
}

JavaScript

(function ( $ ) {

    $.fn.clickOut = function (callback) {

        var that = this;

        $(document).click(function (e) {

            if ( $(e.target).closest(that).size() > 0) {

                return;
            }

            $.isFunction(callback) && $.proxy(callback, that)(e);
        });
    }

})( jQuery )


$(function()
{
    $('#click-out').clickOut(function(){
        $(this).hide()
    });
})