4

I am looking at some javascript code and it has this in a function:

$$('.CssClass').each(function(x) { .... } )

I get that the intent is to apply the anonymous function to each element with a class of CssClass, but I can't work what the $$ refers to ... and can't google for $$!

Update: thanks for the hints. The javascript comes from the iPhone look-alike library: jPint which includes the prototypejs library, and does define $$ as:

function $$() {
  return Selector.findChildElements(document, $A(arguments));
}
Rob W
  • 341,306
  • 83
  • 791
  • 678
Rob Walker
  • 46,588
  • 15
  • 99
  • 136

7 Answers7

15

Probably this prototype function:

$$(cssRule...) -> [HTMLElement...]

Takes an arbitrary number of CSS selectors (strings) and returns a document-order array of extended DOM elements that match any of them.

http://www.prototypejs.org/api/utility#method-$$

Mihai Limbășan
  • 64,368
  • 4
  • 48
  • 59
Tom Haigh
  • 57,217
  • 21
  • 114
  • 142
8

$ is an ordinary symbol character, thus "$", "$$", "$$$" are ordinary variables.

the meaning of $ depends upon the libraries that are in use; in jQuery the $-function creates a jquery object from a css selector, e.g. $("DIV") is a collection of all DIVs in the current document.

mfx
  • 7,168
  • 26
  • 29
4

Are you looking at a library such as mootools by chance? This is used as a short-hand to certain types of objects by accessing the DOM. They do things like $('myElement') to access page elements for example.

Kieran Senior
  • 17,960
  • 26
  • 94
  • 138
  • +1 cause you hit the nail on the head this is likely from a framework. And I like your gravatar. – UnkwnTech Dec 21 '08 at 20:02
  • I believe Prototype has a $$ function as well. – ceejayoz Dec 21 '08 at 20:06
  • I use mootools at work for writing Intranet systems, shorthand techniques like this are handy. I was equally confused by them when I saw them, thinking that they were an operator of some kind. – Kieran Senior Dec 21 '08 at 20:07
1

$ is a valid function name in javascript. So something defines a function $$ that takes a string looking for some class called .CssClass and returns a object where you call each on.

I know that jQuery defines a function called $ at least that does similar things.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • $ being a function name confused me to no end, as it's not a character normally associated with variable names, except perhaps as a prefix in PHP. – Joe Z. Feb 28 '13 at 13:06
1

Any chance you are looking at a MooTools script? http://www.consideropen.com/blog/2008/08/30-days-of-mootools-12-tutorials-day-2-selectors/ (now owned by a domain grabber)

"The $$ lets you quickly select multiple elements and places them into an array (a type of list that lets you manipulate, retrieve, and reorder the list in all sorts of ways). You can select elements by name (such as div, a, img) or an ID, and you can even mix and match."

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Maxfrank
  • 244
  • 2
  • 8
0

Most likely a shorthand function name that handles the DOM accessing of the specified arguments, whether tag name or object id.

As per above, you're likely in MooTools or jQuery.

jerebear
  • 6,503
  • 4
  • 31
  • 38
  • How did I get voted down? That's the same answer as the one that was chosen. $$ is the function name! – jerebear Dec 30 '08 at 15:51
0

In the browser's console, it is another way to write querySelectorAll(). Simply selects all the elements on the web page that you need and puts them in an array.

Practical examples:

Select all the elements and set an outline guide for debugging layouts [source]:

$$('*').map((A,B)=>A.style.outline=`1px solid hsl(${B*B},99%,50%`)

Print the image addresses for all the images on a webpage [source]

$$('img').forEach(img => console.log(img.src))
Amir2mi
  • 896
  • 9
  • 13