$ Variable (Dollar Sign) in Chrome?
Asked Answered
F

6

70

I was working with the developer tools of google chrome on a page without jQuery (or any other library that uses the $ sign as a shortcut). When I inspected $ by the console (by just typing it in and hitting enter), i got this:

$
function () { [native code] }

So, chrome has some native function that can be referenced by $. Only chrome seems to have this one and i cannot access it via window['$'] nor via document['$'] or this['$'].

I was not able to find out what this function is. Do you know what it does and maybe have some background information on this? Thanks in advance!

Fireback answered 2/8, 2012 at 13:29 Comment(6)
Information can be found at developers.google.com/chrome-developer-tools/docs/console and getfirebug.com/wiki/index.php/Command_Line_API.Hammond
What URL & what version of Chrome? I found a page that doesn't use jQuery and does not alias $ (it was surprisingly hard), but I don't see function () { [native code] }. Were you paused at a breakpoint?Mordred
@Matt Ball You can just open about:blank, open the console and type $ then hit enter.Fireback
Possible duplicate of: What is the source of the double-dollar sign selector query function in Chrome /Ffirefox?Tangram
This question was extremely difficult to Google for since Google ignores special characters like $; should we change the title to something that is more SEO-able? Maybe include a phrase like "dollar sign", because that's what I've been using.Valenba
@Valenba Thanks for the suggestion, I added the phrase 'dollar sign' in the title.Fireback
M
65

This has changed yet again, even since just last year.

The devtools console provides $ as an alias to document.querySelector, along with many other things; here's an excerpted list:

  • $(selector) returns the reference to the first DOM element with the specified CSS selector. This function is an alias for the document.querySelector() function.
  • $$(selector) returns an array of elements that match the given CSS selector. This command is equivalent to calling document.querySelectorAll().
  • $_ returns the value of the most recently evaluated expression.
  • The $0, $1, $2, $3 and $4 commands work as a historical reference to the last five DOM elements inspected within the Elements panel or the last five JavaScript heap objects selected in the Profiles panel.

...and a bunch of others.

Note how it calls $ an alias of document.querySelector, but says $$ is "equivalent" to calling document.querySelectorAll. Neither seems to be literally true; $ === document.querySelector is false, and $$ returns an array, not a NodeList.

Mcelhaney answered 14/11, 2017 at 16:42 Comment(1)
Thanks! Keeping this question up to date will be useful for future visitors. :)Fireback
F
29

It is one of the Chrome Developer Tools functions (so not available from the page). You can see documentation for it on the Console page.

It gets an element by a selector.

Firefox implements something similar

Flagstone answered 2/8, 2012 at 13:35 Comment(0)
R
21

The existing answers are outdated, $ is not an alias for document.getElementById or document.querySelector, but a wrapper for querySelector. This wrapper actually takes an optional second argument for the element to query the child of.

For Chrome this family of functions is documented under the Console Utilities API reference:

$(selector [, startNode])

$(selector) returns the reference to the first DOM element with the specified CSS selector. When called with one argument, this function is a shortcut for the document.querySelector() function.

$$(selector [, startNode])

$$(selector) returns an array of elements that match the given CSS selector. This command is equivalent to calling Array.from(document.querySelectorAll()).

$x(path [, startNode])

$x(path) returns an array of DOM elements that match the given XPath expression.

Firefox also documents their implementation of these functions under Web Console Helpers (currently the same except $x allows a 3rd argument).

I don't know if/where Safari/WebKit documents it, but it also implements them the same as Chrome.


Note that these values are only the default values within the console versus the page itself. For example if the page sets the variable by including jQuery that variable will take precedent and the console will use the value from the page itself so that $('p') will return a jQuery object rather than just the first p element.

Riegel answered 15/5, 2016 at 5:57 Comment(2)
Link is outdated, these functions are now documented here. Also note that $$() returns an Array, unlike document.querySelectorAll() which returns a NodeList.Bard
And the current documentation calls $ an "alias" for document.querySelector (although that doesn't seem to be true; at least, $ === document.querySelector is false).Mcelhaney
A
8

Judging by the link to the dev tools it is now uses document.querySelector() rather than just getElementById().

Ambsace answered 19/2, 2013 at 22:3 Comment(0)
P
6

https://developers.google.com/chrome-developer-tools/docs/console

It's just quick access to document.getElementById.

Paranoiac answered 2/8, 2012 at 13:36 Comment(3)
I agree, but also notice that $ == document.getElementById returns false ... odd!Meredithmeredithe
@aaaidan, it's because it just doesn't get element by id, It supports sizzle style selectors too! (not test with complex ones though)Melodee
Ah true. Maybe it references document.querySelector, then?Meredithmeredithe
U
5

Ther're two selectors in Webkit inspectors, the same that Mootools one : $ and $$

You can find some informations on it, here

They're juste here to help you in debug.

Uxmal answered 2/8, 2012 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.