Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    confusion with $ in jquery

    Hey all, I'm a bit new to javascript (coded in C++/Java before)

    I am trying to implement someone else's code and I encountered this (The ...... is the function definition):

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

    From what I understand, the $ has to do with jquery in this case. But can anyone give me some insight on its purpose?

    Thanks.

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,474
    Thanks
    6
    Thanked 980 Times in 953 Posts
    The dollar sign is the general jQuery function shortcut. I can’t explain you the complete implementation but it is used, for example, for the DOM node selectors, like:
    PHP Code:
    $('#example')... // dollar function takes a CSS style selector to address an element in the HTML 
    What you have posted above is a shortcut for the global “DOM ready” function, comparable to classic window.onload but quicker because it executes scripts inside that function once the DOM is ready which is usually the case before the entire page contents have completely loaded (talk about images). It could also be written out as:
    PHP Code:
    $(document).ready(function() {
      ...
    }); 
    I’m saying “shortcuts” because that’s what it is. The actual function is jQuery(), e. g. it could all be written as:
    PHP Code:
    jQuery('#example')...
    jQuery(document).ready(function() {
      ...
    }); 
    The developers just decided to use the dollar sign as shortcut.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by VIPStephan View Post
    the global “DOM ready” function, comparable to classic window.onload but quicker because it executes scripts inside that function once the DOM is ready which is usually the case before the entire page contents have completely loaded (talk about images)

    it's not usually the case, it's always the case.
    when does dom ready ever fire after onload?
    ready is just earlier than onload, not quicker; both events take the same amount of time to fire: 0ms.

    i don't mean to pick nits, but i do think we should be precise when discussing subtle details.

    It should also be pointed out that $(fn) will fire if parsed post-onload, whereas dom ready will have already fired, so fn would never execute if bound to ready, but would in the jQuery example.
    Last edited by rnd me; 06-08-2012 at 04:48 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Tags for this Thread

    Posting Permissions

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