1

Now I am closing the alert box when I clicked on the 'x' but I want to close the alertbox when I click outside of it.

Please see my code here :http://jsfiddle.net/Ur5Xn/

How to close alertbox clicking outside of it?

The jQuery:

$(document).ready(function(){
    function showAlertBox(){
     $("#alert").css("display","inherit");
     $("#content").addClass("back");
    }
    function removeAlertBox(){
        $("#alert").css("display","none");
         $("#content").removeClass("back");        
    }

    $("#alertClose").click(function(){
       removeAlertBox(); 
    });
    $("#alertShow").click(function(){
       showAlertBox(); 
    });
});
Xavi
  • 2,552
  • 7
  • 40
  • 59

4 Answers4

2

This should work: http://jsfiddle.net/LagBE/

$(document).on('mouseup', function (e)
{
    var alert = $('#alert');

     // Make sure neither the alert, 
     // nor anything inside of it was clicked

    if (!alert.is(e.target) && alert.has(e.target).length === 0)
    {
        removeAlertBox();
    }
});
2

try this code

$(document).ready(function(){
    function showAlertBox(){
     $("#alert").css("display","inherit");
     $("#content").addClass("back");
    }
    function removeAlertBox(){
        $("#alert").css("display","none");
         $("#content").removeClass("back");        
    }

    $("#alertClose").click(function(e){
        e.stopPropagation();
       removeAlertBox(); 
    });
    $("#alertShow").click(function(e){
        e.stopPropagation();
       showAlertBox(); 
    });

    $(document).click(function(e){
        removeAlertBox();
    });
});
Sohil Desai
  • 2,940
  • 5
  • 23
  • 35
1

jsfiddle here

html:

<div id="alert">
    <div id='transBG'></div>
    <div id="alertbox">I am an alert box <span id="alertClose">X</span></div>
</div>
<div id="content">
    Content <span id="alertShow">Show Alert Box</span>
</div>

css:

#alert {
    display:none;
}
#alertbox{
    border:1px solid #000;   
    position:fixed;
    top: 50%;
    left: 50%;
    margin-top: -50px;
    margin-left: -100px;
    height:100px;
    width:200px;
     z-index:9;
}
#transBG{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;       
    z-index:8;  
}
.back{
    opacity:0.5;
}

javascript:

$(document).ready(function(){
    function showAlertBox(){
     $("#alert").css("display","inherit");
     $("#content").addClass("back");
    }
    function removeAlertBox(){
        $("#alert").css("display","none");
         $("#content").removeClass("back");        
    }      

    $("#transBG").click(function(){
       removeAlertBox(); 
    });

    $("#alertClose").click(function(){
       removeAlertBox(); 
    });
    $("#alertShow").click(function(){
       showAlertBox(); 
    });
});
Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58
1

Here is the update working code:

$(document).ready(function () {
    function showAlertBox() {
        $("#alert").css("display", "inherit");
        $("#content").addClass("back");
        return false;
    }

    function removeAlertBox() {
        $("#alert").css("display", "none");
        $("#content").removeClass("back");
        return false;
    }

    $("#alertClose").click(removeAlertBox);
    $("#alertShow").click(showAlertBox);
    $('#alert').click(false);
    $(document).click(removeAlertBox);
});

See http://jsfiddle.net/Ur5Xn/34/

Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81