tags:

views:

211

answers:

3

How do I check for a click on everything but a certain object?
I use (not), but I'm not sure how to call the class / id of the entire page.

$(".page").not("#divInfoBox").click(function (event) {

}

What's the correct way in the HTML code to specify page, as I have something like:

<html>
<body>
    <div class="page">  
    </div>
</body>
</html>

What I see is this: Clicking on the top half of the page works; but the bottom of the page, where there is nothing but background color, the click does not register because it is "off the page" -- how do I extend this so the click works everywhere?

Thanks, Michael

+8  A: 

Try this:

$("body:not('#divInfoBox')").click(function (event) {

});

Read the doc page for the not selector:

http://docs.jquery.com/Selectors/not

karim79
The problem with this one is; the "bottom of the page" is already outside of the body tag (which I did not know you could attach clicks to)So the answer that worked for me is the one written by activa. Thanks though!
Michael
@Michael - Oh well, I guess 'body' can be changed to 'html' in my answer, so I'll leave it here for the sake of the not selector.
karim79
A: 

Your missing the ); at the end, should be like this:

$(".page").not("#divInfoBox").click(function (event) {
    // do something
});

As for your question, the 'page' div is not extending to the bottom of the page so you need to register the function to work on the body tag.

Also it looks like you are making a popup appear when certain things are clicked and disappear when clicked off to the side. I would recommend looking for a plugin that will handle this for you. I know there are several. Personally I like jqModal.

MitMaro
+1  A: 

You can add a click event to the "html" element. That way you get events even if you're "off" the page:

$("html").click( function() { ...  } );

Demo can be seen here: http://jsbin.com/eguti

Philippe Leybaert