SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Location
    San Francsico, CA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a question about $ function

    Hi guys,

    I'm studying Javascript and I read a $ function like this

    Code:
    var $ = function(id){
         return document.getElementById(id);
    }
    According the book, it says this is shortcut for the document.getElementById method. I can't understand why is that?? why not use document.getElementById("id") directly? and instead of this, creating a $ function and then call the function? what's the benefit for this??

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,615
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    It's shorthand. It's shorter and simpler to use. Less code to type. Less data to transfer when a page is loaded.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    document.getElementById() is perhaps the most commonly used call in JavaScript. Since just about any script can potentially contain dozens of such calls it makes sense to provide a shorter name that can be used to perform that processing. A single character function name is as short as you can get and $ was considered to be the one character that is least likely to be used as a single letter function name in existing scripts.
    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="^$">

  4. #4
    SitePoint Member
    Join Date
    Jul 2010
    Location
    San Francsico, CA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    It's shorthand. It's shorter and simpler to use. Less code to type. Less data to transfer when a page is loaded.
    So is it with the same meaning that using Jquery?? If I have linked Jquery lib, then I can do the same thing and without create a $ function. Am I right??

  5. #5
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,615
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Essentially, yes, however jQuery has additional ways you can work with selecting elements using the $
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  6. #6
    SitePoint Member
    Join Date
    Jul 2010
    Location
    San Francsico, CA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    Essentially, yes, however jQuery has additional ways you can work with selecting elements using the $
    I see. For additional ways, do you mean the methods being able to access specific node or certain parent node's children, something like that? If it's not, would you mind give me some clues so that I will know where I can find and study it. Thx.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Here's the jQuery documentation about its selectors.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    With jQuery you can put any CSS selector in the $ function and it will return the matched elements. Even complex queries like this:

    Code:
    $('#wrapper div:first-child:not(.section) > span ~ a[href^=mailto]');
    For more info, look at the jQuery documentation for the function.


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
  •