[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1983
  • Last Modified:

jQuery: what is $("*", document.body).click(function (event) {...});


I saw this in the jquery doc page:

Just wondering what is the '*' asterisk, document.body?
What does the second parameter do? Is is a relative DOM?

$( "*", document.body ).click(function( event ) {
  var offset = $( this ).offset();
  $( "#result" ).text( this.tagName +
    " coords ( " + offset.left + ", " + offset.top + " )" );

Open in new window

Much thanks and Father bless<><
Victor Kimura
Victor Kimura
  • 2
3 Solutions
Alexandre SimõesManager / Technology SpecialistCommented:
jQuery selectors are DOM selectors, which means they comply with CSS selectors (in most cases).
So this said, * means every single DOM Element on the page, just like * means in CSS.

The second parameter defines the scope of the selector. In this case is the document.body, which doesn't add much in terms of logic... it will still match everything on the page.
Alexandre SimõesManager / Technology SpecialistCommented:
Ah sorry, the second parameter here actually adds something.
It prevents the body itself from getting the click event handler.

Here's a simple example: http://jsfiddle.net/AlexCode/xUvm4/
check the first one : http://api.jquery.com/category/selectors/

yes "*" mean everything
the second parameter specify the context, everything in what, from where?

test page : http://jsfiddle.net/ABLhx/1/

<div id="only">

$("*", document.body) // all inside the body
$("*", "#only") // all inside the div#only

Open in new window

Victor KimuraAuthor Commented:
I see. Thank you, fellas! That's helpful. =)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now