23

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?

8
  • 2
    I don't know where you're getting this. 1 and 3 work the same for me. i.imgur.com/2YBC2Ad.png (with this fiddle jsfiddle.net/f9teqyo3) Feb 29, 2016 at 10:14
  • @FlorianMargaine yep, it's not reproducible for my as well. I have observed this issue on internal web page we have. Can't really share it...
    – Johnny
    Feb 29, 2016 at 10:36
  • Then I'd like to give you a link someone else already gave you :) stackoverflow.com/help/mcve Feb 29, 2016 at 11:05
  • We can't really help you otherwise. Feb 29, 2016 at 11:05
  • That said, my guess is that your page is using prototype.js which is reusing the $$ variable. Feb 29, 2016 at 11:06

1 Answer 1

52

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.

16
  • Thanks. So the difference in the need, for example, for apostrophe around attribute value in $$ case in opposite to $ case, from the difference between how querySelector() and querySelectorAll() works?
    – Johnny
    Feb 28, 2016 at 14:55
  • @StasS I edited my answer. Does that answer your question ? If it doesn't, please provide more code demonstrating your problem. Feb 28, 2016 at 16:39
  • Thanks Denys but i'm asking something a bit different. Edited my code to demonstrate it. There is a syntax that would work in $ case but not in $$. And as far as i understand, there should be some consistency in the underlaying implementation code.
    – Johnny
    Feb 28, 2016 at 18:24
  • 4
    I am here in 2017 and can confirm that Trump is indeed useless, have another upvote
    – Callat
    Aug 18, 2017 at 3:07
  • 1
    Chrome doc link no longer contains this info, but Utilities API Reference sub-section does. See also matching Firefox docs. Aug 23, 2020 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.