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

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

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

Hi,

I saw this in the jquery doc page:
http://api.jquery.com/offset/

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

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

Open in new window


Much thanks and Father bless<><
0
Victor Kimura
Asked:
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.
0
 
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/
0
 
leakim971PluritechnicianCommented:
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/

<body>
<div id="only">
    <div>hello</div>
</div>
<div>world</div>
<body>

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

Open in new window

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

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