SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $(document).ready(); vs $('document').ready(); what is the difference?

    Can someone please explain to me the difference between

    $(document).ready(function(){...});

    and

    $('document').ready(function(){...});

    Notice that there are no quotes within the first document block.

    Thanks

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,868
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    $(document) passes the document element to JQuery.

    $('document') passes the CSS selector 'document' to JQuery.

    Neither is necessary as the ready method in jQuery should no longer be called directly.

    You should either use:

    $(function(){...});

    which implicitly runs the ready method or if the JavaScript is at the bottom of the page you can use:

    (function($){...})(jQuery);

    and so run the code straight away without needing to test ready.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you for the detailed explanation


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •