Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "$" sign mean in jQuery or JavaScript? [duplicate]

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

Now this must seem to be an easy and stupid question to ask but I must know why 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.

like image 970
GeekMasher Avatar asked Dec 29 '11 12:12

GeekMasher


People also ask

What does the $() mean in jQuery?

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.

What does '$' mean in JavaScript?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

What does dollar sign ($) mean in jQuery?

The $ sign is nothing but an identifier of jQuery() function. Instead of writing jQuery we simply write $ which is the same as jQuery() function. A $ with a selector specifies that it is a jQuery selector. It is given a shorter identifier as $ just to reduce the time for writing larger syntax.

What does ++ mean in jQuery?

The ++ notation is the increment operators. i++ is the same thing of. i = i +1.


2 Answers

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".

like image 80
Quentin Avatar answered Oct 11 '22 14:10

Quentin


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'); }); 
like image 36
dflemstr Avatar answered Oct 11 '22 14:10

dflemstr