Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close javascript popup by clicking anywhere

I am super novice and need a quick advice. I have installed a javascript based popup plugin on my wordpress site, witch opens automatically when somebody visits the site. To close the popup the user will have to click a cross X in the corner of the popup.

I need to edit the plugin so that the user can click ANYWHERE, and the plugin will close.

Here is the javascript code I found. any tips about this?

function open_lightbox(){
    //var closebut = (typeof(ujiPopups) !== 'undefined' && ujiPopups != null && ujiPopups.showclose && ujiPopups.showclose == "true") ? true : false;
    jQuery("#popup").modal({
        onOpen: function (dialog) {
            dialog.overlay.fadeIn('fast');
            dialog.data.hide();
            dialog.container.show('fast', function () {
                dialog.data.fadeIn('slow');  
            }); 
        },
        autoResize: false,
        autoPosition: true,
        escClose: false,
        zIndex: 999999,
        overlayClose: false
    });     
}

function popups_close(){
    jQuery.modal.close();
    jQuery("#popup").remove();
}   
like image 541
Tormod Ndsn Avatar asked Mar 22 '23 19:03

Tormod Ndsn


2 Answers

Something like this should do it:

$(document).click(function() { 
    if($('#popup').is(':visible')) {
        popups_close();
    }
});

If you wish to keep the modal active on interaction with the popup itself:

$(document).click(function(e) {
    if (!$(e.target).is("#popup")) {
        if ($('#popup').is(':visible')) {
            popups_close();
        }
    }
});

A simple example here: http://jsfiddle.net/wnT4G/

*Check comments for some elegant revisions by @ComFreek

like image 96
Stuart Kershaw Avatar answered Mar 29 '23 06:03

Stuart Kershaw


I use a rather strange method, but it works:

$('.click-btn').click(function(){
   $('.modal').show(); //show popup
})

$('body').click(function(){
   $('.modal').hide(); //hide modal
})

$('.click-btn, .modal').click(function(e){
   e.stopPropagation; // don't close modal by clicking inside modal and by clicking btn
})
like image 37
BordiArt Avatar answered Mar 29 '23 06:03

BordiArt