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.
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.
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.
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.
The ++ notation is the increment operators. i++ is the same thing of. i = i +1.
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".
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'); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With