tags:

views:

155

answers:

5

I'm trying to catch an event when a user clicks a blank space on the page with jQuery. For example, lets say you have the following:

<html>
<head>
    <title>Test</title>
</head>
    <body>
        <p>Here's some text!</p>
    <body>
</html>

I want to catch the event when the user clicks on anything but the p tag.

Is there a way to catch something like this?

+2  A: 

You could observe the document for onclick, and then check the event.currentTarget in your event handling function.

nfm
A: 

You can do something like this (I think)

$('body').not('p').click(function(){});

Not sure how well that will work, though.

theIV
I believe that will just filter the selector and won't influence the click event bubbling.
MacAnthony
Cool. At least I learned something from this! :) Thank you.
theIV
+3  A: 

This seems to work:

$(function() {
    $('body').click(function() {
     alert('clicked the page');
     return false;
    });
    $('*:not(body)').click(function() {
     alert('clicked an item!');
     return false;
    });
});

One "problem" you'll have is that paragraphs are block elements by default, so clicking to the side of a sentence will produce an item click even if it looks like you haven't clicked on an item.

great_llama
+2  A: 

This works in chrome. Haven't tried other browsers. Works off the idea that stickmangumby had:

$('body').click( function (e) { 
    if ( e.target == this ) 
        alert('works'); 
});
MacAnthony
Ahh, I forgot all about the target attribute. This fits in perfectly with what I got already. All of the other answers work as well, I just think that this one is the best for just handling the one event of handling clicks on the body(or in my case, the html tag level)
Adam Mika
I just want to point out that this will not work for every point on the screen unless <body> is at least 100% of the window's width and height.
Josh Leitzel
correct, which is why I switched it from the $('body') selector to the $('html') selector in my code to avoid manually changing the body's height and width.
Adam Mika
+1  A: 

I'm making a little web app that is similar to Taskpaper (for the Mac) and I want it to mimic the application's behavior. More of a proof of concept than anything else

In light of your comments, I wouldn't worry about the body specifically then. Instead wire up a div so that you can constrain the area you want to react to events. In the case where there's no other elements on the page, it's the same thing.

For what it's worth, on any web page, your users would hate you if clicking any whitespace anywhere does something - it would be a poor UI experience.

Robert Paulson
I think that depends greatly on what he's planning on doing when the event fires. It's pretty presumptuous to say what he's doing is bad if you don't know what he's doing.
MacAnthony
Ah but I did ask as a comment why and I do have a good idea of what he's trying to do, which is why it's prefaced with "in light of your comments". I also think it's a bad idea to hook into the body-click event, even if it's a proof of concept.
Robert Paulson
"your users would hate you if clicking any whitespace anywhere does something - it would be a poor UI experience"Actually, that's exactly what Taskpaper does. Anywhere you click in it's main window, you either are entering a new task or are editing something that is already on the screen.
Adam Mika