Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double dollar $$() vs Dollar sign $() in Chrome console behavior

In our project, there is a different functionality when one Dollar sign used $() in Chrome console vs two Dollar signs $$(), besides the known difference that $$() return an array an $() return the first element.

For example, selector for specific element, with one dollar and two dollar queries:

$$(".my-class[my-attribute='trump']") //works

$('.my-class[my-attribute=sanders]') //works

$$('.my-class[my-attribute=trump]') //not work

What is the source and explanation for this behavior?

like image 507
Johnny Avatar asked Feb 28 '16 13:02

Johnny


People also ask

What is $$ in chrome console?

$ is an alias for document. querySelector . In the same vein there is $$ which is an alias for document. querySelectorAll . It is defined in the Command line (console) api.

What does dollar sign in JavaScript mean?

$ – dollar identifier The dollar ($) sign is a JavaScript identifier, which simply means that it identifies an object in the same way that a name or variable does. Variables, functions, properties, events, and objects can be identified by the $ sign.

How do I use console commands in Chrome?

To open the Command Menu: Press Control + Shift + P (Windows / Linux) or Command + Shift + P (Mac). Click. Customize and control DevTools and then select Run command.

What does the dollar sign in jQuery mean?

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.


1 Answers

From Chrome Developer Tools documentation:

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.

When you use querySelector (or $), the result is an element or null. When you use $$, the result isn't an element but an Array which can be easily iterated over. This differs from the native querySelectorAll where it returns a NodeList which is slightly harder to go over all the entries.

Regarding the quote: of course it works the same. See:

enter image description here

Conclusion: It's useless to quote trump. You might also end insane.

like image 123
Denys Séguret Avatar answered Sep 20 '22 08:09

Denys Séguret