11

I'm unable to trigger a click on the body tag using jQuery using this:

$('body').click();

Even this fails:

$('body').trigger('click');
pppery
  • 3,731
  • 22
  • 33
  • 46
ruturaj
  • 3,343
  • 4
  • 19
  • 11
  • Could it be that you are triggering the click event before you've defined the click catcher function ? show us the code where you handle the click. – duckyflip May 28 '09 at 10:56
  • Don't want to bring up something old but I would guess that the default body height of a document isn't the same as the viewport height. So using css to set the height to 100% will fix this, e.g. `html, body{ width: 100%; height: 100%; }` – Rob Farr Jun 03 '13 at 10:21

5 Answers5

11

You should have something like this:

$('body').click(function() {
   // do something here
});

The callback function will be called when the user clicks somewhere on the web page. You can trigger the callback programmatically with:

$('body').trigger('click');
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • I've already done what u've mentioned.. When I personally click anywhere on the page, the click Handler works perfectly – ruturaj May 28 '09 at 08:08
  • do you mean you're trying to _fire_ the event? instead of firing the event, why not call the function your click handler would? – Jonathan Fingland May 28 '09 at 08:12
  • 1
    yes I'm trying to fire the event, I want to simulate a click on the body. – ruturaj May 28 '09 at 08:53
  • I'm writing a unit test using QUnit (http://docs.jquery.com/QUnit) and hence I've got to fire the event and then check if the handler worked as it was supposed to work. – ruturaj May 28 '09 at 09:11
10

Interestingly, when I replaced this:

$("body").trigger("click")

With this:

jQuery("body").trigger("click")

It works!

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
ruturaj
  • 3,343
  • 4
  • 19
  • 11
  • 16
    that indicates to me then that you're using other JavaScript frameworks on the page that also have a $() shorthand defined function. – Russ Cam Sep 14 '09 at 09:44
  • or another version of JQuery in a related script.. I got it when using JQGrid when referencing .each() – KevinDeus Aug 20 '12 at 23:53
7

I've used the following code a few times and it works sweet:

$("body").click(function(e){ 
    // Check what has been clicked:
    var target = $(e.target); 
    if(target.is("#target")){
    // The target was clicked
    // Do something...
  }
});
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
Fermin
  • 34,961
  • 21
  • 83
  • 129
2

if all things were said didn't work, go back to basics and test if this is working:

<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
  </head>
  <body>
    <script type="text/javascript">
      $('body').click(function() {
        // do something here like:
        alert('hey! The body click is working!!!')
      });
    </script>
  </body>
</html>

then tell me if its working or not.

Seeker
  • 365
  • 1
  • 6
  • 16
  • hmmm... just note u answer your own question X). anyway u can do alert($.toString()); to give you a clue of what is the other "plugin" that is interfering with jQuery. – Seeker Sep 06 '10 at 23:27
0

As mentioned by Seeker, the problem could have been that you setup the click() function too soon. From your code snippet, we cannot know where you placed the script and whether it gets run at the right time.

An important point is to run such scripts after the document is ready. This is done by placing the click() initialization within that other function as in:

jQuery(document).ready(function()
  {
    jQuery("body").click(function()
      {
        // ... your click code here ...
      });
  });

This is usually the best method, especially if you include your JavaScript code in your <head> tag. If you include it at the very bottom of the page, then the ready() function is less important, but it may still be useful.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156