views:

4021

answers:

5

I would like to know if this is the correct way of hiding visible elements when clicked anywhere on the page.

$(document).click(function (event) {      
    $('#myDIV:visible').hide();
});

The element (div, span etc) cannot disappear when click event occurs withing the boundaries of the element.

+5  A: 

Try this

 $('.myDiv').click(function(e) { //button click class name is myDiv
  e.stopPropagation();
 })

 $(function(){
  $(document).click(function(){  
  $('.myDiv').hide(); //hide the button

  });
});

I use class name instead of ID, because in asp.net you have to worry about the extra stuff .net attaches to the id

EDIT- Since you added a another piece, it would work like this:

 $('.myDiv').click(function() { //button click class name is myDiv
  e.stopPropagation();
 })

 $(function(){
  $('.openDiv').click(function() {
  $('.myDiv').show(); 

  });
  $(document).click(function(){  
  $('.myDiv').hide(); //hide the button

  });
});
TStamper
you have it exactly opposite. You can add and remove classes from elements, Id's never change
Jeremy B.
no, you have it opposite, I was referring to when using asp.net server controls.. the ids change you use the runat=server attribute
TStamper
read my comment, and read OP's more carefully.
Jeremy B.
just to make you happy, I changed the wording
TStamper
e.stopPropagation(); seems to be the trick.
phirschybar
A: 

This doesn't work - it hides the .myDIV when you click inside of it.

$('.openDiv').click(function(e) {
$('.myDiv').show(); 
e.stopPropagation();
})

$(document).click(function(){  
$('.myDiv').hide(); 

});

});

<a class="openDiv">DISPLAY DIV</a>

<div class="myDiv">HIDE DIV</div>
Franek
so you want the div to showto show when the link is placed,then take that e.stopPropa.. out of there and follow my option
TStamper
+14  A: 

if i understand you want to hide a div when you click anywhere but the div, and if you do click while over the div, then it should NOT close. you can do this:

$(document).click(function() {
    alert("me");
});
$(".myDiv").click(function() {
    return false;
});

This binds the click to the entire page, but if you click on the div in question it will cancel the click event.

Jeremy B.
no, you have it opposite, I was referring to when using asp.net server controls.. the ids change you use the runat=server attribute
TStamper
no, you were referring to something the OP never even alluded to he made no mention of asp.net, and the wonderful ctl_... id naming philosophy. Other than that, HTML rules stated that ID's do not change, and since we're talking javascript, classes change.
Jeremy B.
what, when do classes change unless when you change them yourself, I was referring to my preference
TStamper
just to make you happy, I changed the wording
TStamper
I am not trying to be rude, but for people not using asp.net and who are new to html/javascript we don't want to confuse them.
Jeremy B.
+1  A: 

This following code example seems to work best for me. While you can use 'return false' that stops all handling of that event for the div or any of it's children. If you want to have controls on the pop-up div (a pop-up login form for example) you need to use event.stopPropogation().

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
</head>
<body>
    <a id="link" href="#">show box</a>
    <div id="box" style="background: #eee; display: none">
        <p>a paragraph of text</p>
        <input type="file"  />
    </div>

    <script src="jquery.js" type="text/javascript"></script>

    <script type="text/javascript">
        var box = $('#box');
        var link = $('#link');

        link.click(function() {
            box.show(); return false;
        });

        $(document).click(function() {
            box.hide();
        });

        box.click(function(e) {
            e.stopPropagation();
        });

    </script>
</body>
</html>
A: 

Thanks, Thomas. I'm new to JS and I've been looking crazy for a solution to my problem. Yours helped.

I've used jquery to make a Login-box that slides down. For best user experience I desided to make the box disappear when user clicks somewhere but the box. I'm a little bit embarrassed over using about four hours fixing this. But hey, I'm new to JS.

Maybe my code can help someone out:

<body>
<button class="login">Logg inn</button>
<script type="text/javascript">

    $("button.login").click(function () {
        if ($("div#box:first").is(":hidden")) {
                $("div#box").slideDown("slow");} 
            else {
                $("div#box").slideUp("slow");
                }
    });
    </script>
<div id="box">Lots of login content</div>

<script type="text/javascript">
    var box = $('#box');
    var login = $('.login');

    login.click(function() {
        box.show(); return false;
    });

    $(document).click(function() {
        box.hide();
    });

    box.click(function(e) {
        e.stopPropagation();
    });

</script>

</body>
Tommy