www.webdeveloper.com
+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2006
    Posts
    146

    Dollar $ in Javascript

    I have some new code that i am responsible for, and do not know what the $ does in javascript. Can someone enlighten me in this ?

    For example, see the code below:

    Code:
    function shiptostateChange(shipgroup, isUpgradedShippingExempt)
    {
            // If the business unit is UK, disable the shipping method box if country is changed to anything else
            // If the business unit is US, disable the shipping method boxes if country is changed to anything else
            
            stateChange($("State" + shipgroup), shipgroup)      
    	    disableExpress($('Country' + shipgroup),shipgroup,isUpgradedShippingExempt)
    	        
    }
    what are the dollar symbols? Are they ajax related ?
    
    (Thank you)

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    libraries such as Prototype.js and jQuery use $ to represent getElementById()

    Look at what library is being used on the page and look at the documentation for it.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  3. #3
    Join Date
    Mar 2007
    Location
    Athens, Greece
    Posts
    177
    Hi,

    The dollar symbol usually found on regular expressions (eg. as back-reference for a match found in a string ).
    Here is a function named: $ and used to getElementById() (as already reported)
    (search the code for a function with this name)

    Cheers!

    Kostas
    Last edited by Kostas Zotos; 07-16-2008 at 02:14 PM.
    K. Zotos online portfolio (3D, Web, Programming): http://www.in3d.eu/

  4. #4
    Join Date
    Apr 2008
    Posts
    61
    I had this quesiton b4 and now I understand too.

    usually, people use $ for two reasons (as far as i know, please correct me if wrong)
    1) able to port to a PHP script
    2) jQuery's convention of using $ for function / var name.

  5. #5
    Join Date
    Aug 2007
    Posts
    3,764
    Prototype had it first (their $ function is quite handy, it accepts either id strings or elements and returns an array). Everyone else thought it was a good idea so copied it.
    Great wit and madness are near allied, and fine a line their bounds divide.

  6. #6
    Join Date
    Jul 2008
    Posts
    9
    Quote Originally Posted by A1ien51
    libraries such as Prototype.js and jQuery use $ to represent getElementById()

    Look at what library is being used on the page and look at the documentation for it.

    Eric
    Hi Eric,

    Actually, the "$" is a shorthand for the jQuery namespace and is used to reference the global jQuery object.

    So while many folks can use it like $( "#mydiv" ), you can also use it like jQuery( "#mydiv" ).

    Rey
    jQuery Project Team

  7. #7
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,973
    There are 27 letters in the alphabet as far as JavaScript is concerned. a-z and $. Anywhere you can use a letter in JavaScript you can use $ as that letter. The only place where it has a special meaning beyond whatever your code defines it to mean is where it appears in a regular expression.

    You can always define

    function $() {alert('you are so smart');}

    and then every reference to $() in your code will run that function. If you do define a function with a single letter name then that function can do whatever you want it to do. Any special meaning for such a function will be determined by which library of functions you use with your code (if any).
    Stephen

  8. #8
    Join Date
    Aug 2007
    Posts
    3,764
    There's underscore too.
    Code:
    function _(a) {
    alert(a);
    }
    _("Ridiculous name for a function");
    Great wit and madness are near allied, and fine a line their bounds divide.

  9. #9
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,973
    Oops, forgot that one - JavaScript has a 28 letter alphabet.
    Stephen

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

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
HTML5 Development Center



Recent Articles