1

Made this custom alert box:

<script type="text/javascript">
    $(function () {
        var $alert = $('#alert');
        if ($alert.length) {
            var alerttimer = window.setTimeout(function () {
                $alert.trigger('click');
            });
            $alert.animate({ height: $alert.css('line-height') || '80px' }, 200).click(function () {
                window.clearTimeout(alerttimer);
                $alert.animate({ height: '0' }, 200);
            });
        }
    });
</script> 

I want it to be open until the user chooses to click on it or anywhere else on the screen. How do I make this happen?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Ismailp
  • 2,293
  • 4
  • 36
  • 65

3 Answers3

2

Assuming that clicking anywhere (in, or out, of the alert box itself) is supposed to hide/remove the alert:

$('body').click(
    function(){
        if ($alert.is(':visible')){
            $alert.hide();
        }
    });

should work, I think.

David Thomas
  • 240,457
  • 50
  • 366
  • 401
1

If you want to justs get rid of it, try calling the hide() function when an onclick event is triggered.

$.click(function() {
  $alert.hide();
});
gotomanners
  • 7,563
  • 1
  • 23
  • 38
0

The top answer here is excellent IMO. It covers the part about anywhere else on the screen.

The way to do it is to bind a click event to the body and stop propagation on any event that happens inside the area that isn't supposed to trigger closing the alert.

Community
  • 1
  • 1
Benjamin Atkin
  • 13,277
  • 7
  • 54
  • 56