$ Variable (Dollar Sign) in Chrome?

JavascriptGoogle ChromeGoogle Chrome-DevtoolsDollar Sign

Javascript Problem Overview


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!

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

Solution 3 - Javascript

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.

This family of functions is documented under the Console: Selecting Elements:

> Selecting Elements > > There are a few shortcuts for selecting elements. These save you valuable time when compared to typing out their standard counterparts. > > $() Returns the first element that matches the specified CSS selector. It is a shortcut for document.querySelector(). > > $$() Returns an array of all the elements that match the specified CSS selector. This is an alias for document.querySelectorAll() > > $x() Returns an array of elements that match the specified XPath.


However, these values are only the default values in the console. If the page overwrites the variable by including something like jQuery, the console will use the value from the page itself, and something like $('p') will return a jQuery object rather than just the first p element.

Solution 4 - Javascript

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

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

It's just quick access to document.getElementById.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDennisView Question on Stackoverflow
Solution 1 - JavascriptT.J. CrowderView Answer on Stackoverflow
Solution 2 - JavascriptQuentinView Answer on Stackoverflow
Solution 3 - JavascriptAlexander O'MaraView Answer on Stackoverflow
Solution 4 - JavascriptAlbertEngelBView Answer on Stackoverflow
Solution 5 - JavascriptzessxView Answer on Stackoverflow
Solution 6 - JavascriptoryolView Answer on Stackoverflow