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

Posted on 2014-07-14
Last Modified: 2014-07-14

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 + ", " + + " )" );

Open in new window

Much thanks and Father bless<><
Question by:Victor Kimura
    LVL 30

    Accepted Solution

    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.
    LVL 30

    Assisted Solution

    by:Alexandre Simões
    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:
    LVL 81

    Assisted Solution

    check the first one :

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

    test page :

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

    Open in new window


    Author Closing Comment

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

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    Join & Write a Comment

    What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
    Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now