76

Possible Duplicate:
What is the meaning of “$” sign in JavaScript

Why do we use the dollar ($) symbol in jQuery and JavaScript? I always put a dollar in my scripts but I actuary don't know why.

For an example:

$('#Text').click(function () {
  $('#Text').css('color', 'red')
});

This just changes the text colour when you click it, but it demonstrates my point.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
GeekMasher
  • 1,066
  • 2
  • 11
  • 17
  • Thanks all, I know it was a stupid question! – GeekMasher Dec 29 '11 at 12:46
  • 2
    Not necessarily a stupid question. Just do some research ahead of time. You'd be surprised, a google search will more often than not result in links to questions/answers on stackoverflow – Dexygen Dec 29 '11 at 14:42
  • 35
    Guys you are sometimes to hard on posters. I'm always happy when I google something and I have a well documented SO Q&A page with all of my stupid questions answered as a first result. – cedbeu May 14 '13 at 22:16
  • 9
    There are no stupid questions period. – amar Jul 14 '15 at 10:50
  • @GeekMasher there's no stupid questions, only unprepared ones. – carloswm85 Jun 29 '22 at 12:46

6 Answers6

62

In JavaScript it has no special significance (no more than a or Q anyway). It is just an uninformative variable name.

In jQuery the variable is assigned a copy of the jQuery function. This function is heavily overloaded and means half a dozen different things depending on what arguments it is passed. In this particular example you are passing it a string that contains a selector, so the function means "Create a jQuery object containing the element with the id Text".

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
48

The $ is just a function. It is actually an alias for the function called jQuery, so your code can be written like this with the exact same results:

jQuery('#Text').click(function () {
  jQuery('#Text').css('color', 'red');
});
dflemstr
  • 25,947
  • 5
  • 70
  • 105
16

In jQuery, the $ sign is just an alias to jQuery(), then an alias for a function.

This page reports:

Basic syntax is: $(selector).action()

  • A dollar sign to define jQuery
  • A (selector) to "query (or find)" HTML elements
  • A jQuery action() to be performed on the element(s)
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
8

The jQuery syntax is tailor made for selecting HTML elements and perform some action on the element(s).

Basic syntax is: $(selector).action()

A dollar sign to define jQuery A (selector) to "query (or find)" HTML elements A jQuery action() to be performed on the element(s)

More on this

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
2

The $ symbol simply invokes the jQuery library's selector functionality. So $("#Text") returns the jQuery object for the Text div which can then be modified.

Jeankowkow
  • 814
  • 13
  • 33
  • 2
    Not entirely true. It *might* invoke the selector functionality, it depends what arguments you pass it. – Quentin Dec 29 '11 at 12:15
1

Additional to the jQuery thing treated in the other answers there is another meaning in JavaScript - as prefix for the RegExp properties representing matches, for example:

"test".match( /t(e)st/ );
alert( RegExp.$1 );

will alert "e"

But also here it's not "magic" but simply part of the properties name

Matteo B.
  • 3,906
  • 2
  • 29
  • 43
  • 3
    From the context, that clearly isn't the case here. (And that code throws an exception for me) – Quentin Dec 29 '11 at 12:19
  • @Quentin It's sort of the case since the question just was what the `$` sign means. This is a part of the complete answer. And sorry, you were right, it did throw an exception since I had a Typo in spelling `RegExp` but now it's correct. – Matteo B. Dec 29 '11 at 12:24