0

I've got some code that works fine - I adapted it from something else (this is the simplified version, not production, of course):

$(".colorMe td").each(function() {
var val = (this.innerHTML);
if (val == "Yes") {
    this.style.backgroundColor = "#F00000";
} 
});

See also http://jsfiddle.net/GAwrB/221/

I don't know what the $ means though. I've seen lost of other posts saying that it might have something to do with jQuery, but I can't find any definitive answer. I'm sure that other newbies like me would find it useful to know.

Edit:

This question is different from Why does JQuery have dollar signs everywhere? as I initially didn't know that the $ meant jQuery - so the previous question isn't useful for anyone like me, who'd new to javascript.

I also see that there are basically two different answers given - some say that $ is a variable name, while others say that it calls jQuery - look to me that the jQuery answer is right here - in which case, I don't understand why some people say that it's a variable name - I assume that that's right in some cases.

Community
  • 1
  • 1
Chris
  • 46
  • 5
  • 1
    jQuery takes over the $ variable as an alias to the jQuery function. that could be written as `jQuery('.colorMe td').each(function() {` – Jeff Shaver Feb 10 '14 at 13:35
  • its only a variable... you can do also: var $ = "asdf"; – Cracker0dks Feb 10 '14 at 13:36
  • 2
    Worth noting: you can tell jQuery not to assign $ to the jQuery() function (using `jQuery.noConflict()`). So the absence of dollar signs doesn't necessarily mean jQuery isn't being used (and of course the presence of them could be from MooTools or another library and doesn't necessarily mean jQuery is being used either). – Matt Browne Feb 10 '14 at 13:40
  • @T.J.Crowder not specifically a duplicate of that. That question seems to be about using `$` signs in the naming convention for jQuery object variables – Gareth Feb 10 '14 at 13:42
  • 1
    @Gareth: You're right, missed the last sentence. Sadly the one Praveen pointed to *also* asks primarily about the use of `$` in the name. I'm sure there's a previous question asking what this one does... – T.J. Crowder Feb 10 '14 at 13:44

3 Answers3

7

Short answer: it's impossible to tell. $ is just another valid variable name, you'll need to look elsewhere to see what other Javascript is included which might be defining that function.

By the context, it's probably jQuery, but you'll be only able to tell for sure by seeing whether jQuery is being included on the page.

You might as well ask "What does foo mean in this code?"

Gareth
  • 115,773
  • 31
  • 143
  • 154
  • There isn't anything else that I added - it seems to work out of the tin, as it were. – Chris Feb 10 '14 at 13:37
  • 1
    Chris, that's a jsFiddle feature. The left-hand bar of your jsFiddle page indicates that jQuery is automatically being included in your fiddle results – Gareth Feb 10 '14 at 13:39
  • As a newbie, I tried changing $ for 'foo' (for sake of argument) - that didn't work, so it does seem that the $ has a specific meaning here - my understanding is growing greatly now - if I'm right, it is a variable, but jQuery uses the $ variable to call functions - is that right? I hope that this will be useful to other newbies :) – Chris Feb 11 '14 at 05:22
  • @Chris: Consider these two things: 1) `$` *is* just a variable name, but 2) functions in Javascript can be stored in variables. – Gareth Feb 11 '14 at 13:49
3

$ is a variable name. It has no special meaning in JavaScript.

A number of libraries (such as MooTools, Prototype.js, Zepto and jQuery) assign a function to it. Since it is followed by (arguments) it is a function (or an error!).

Since the return value of that function is an object with an each method, and that jQuery is ludicrously popular, it is probably jQuery.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
0

It's jQuery. It's used to refer to an object with the given attributes. i.e.: $(".a").click(); is invoking click for an object with class name "a" (the dot is class name, has tag is ID).

Shahar
  • 1,703
  • 2
  • 11
  • 18