1

This seems like such a basic question, but I swear I can't see it in the jQuery documentation.

I've inherited some code that does:

 $('.tag', true);

The jQuery docs simply say:

jQuery( selector [, context ] )

And 'true' isn't a context in this case. Is this something to do with bubbling? What does 'true' do in a selector, and where does the documentation cover this use case?

Edit: As A. Wolff notes below, this isn't jQuery, it's an alias for document.querySelector & querySelectorAll.

function $(selector, all) {
    return base['querySelector'+(all?'All':'')](selector);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • 9
    Maybe the person writing that code never actually looked at the docs like you did and just put in `true`. Do you observe any effects? – Knelis Jun 18 '15 at 12:19
  • 4
    When I test this, I get back no results, because `true` is actually being used a context, and obviously it doesn't make any sense for `true` to have any DOM elements whatsoever. – apsillers Jun 18 '15 at 12:21
  • 1
    What version of jquery is it? Is/was it built using an old version? Do you get any errors in the console? That selector looks incorrect. – Liam Jun 18 '15 at 12:21
  • 7
    Are you sure `$` refers to jQuery? Provide more context regarding your posted code. How is it used? Etc... – A. Wolff Jun 18 '15 at 12:22
  • $('.tag', false) and $('.tag') returns the same,while true returns nothing – Jayababu Jun 18 '15 at 12:22
  • 3
    @Jayababu That's because all falsey context arguments are treated as "use the default context". (`false`, `0`, `""`, `null`, `undefined`). A missing argument is the same as `undefined`, and for this argument, jQuery treats all falsey values the same. – apsillers Jun 18 '15 at 12:24
  • 2
    `true` is cannot be considered a "context" so it would simply be ignored. See https://github.com/jquery/jquery/blob/master/src/core/init.js#L87 – haim770 Jun 18 '15 at 12:30
  • Here is the same question: http://stackoverflow.com/questions/16422478/what-is-context-in-jquery-selector – Emmanuel Delay Jun 18 '15 at 12:32
  • 2
    @A.Wolff **You're correct - it's a custom function and not jQuery**. If you add this an an answer I'll mark it as correct. – mikemaccana Jun 18 '15 at 12:35
  • 1
    @mikemaccana, do you mean to tell me they intentionally mixed custom code(aliased with $) and jQuery? Ahhhhhhhhhhhh! – AmmarCSE Jun 18 '15 at 12:41

2 Answers2

3

So $ isn't referring to jQuery but to custom method. Then the true parameter has nothing to do with context. :)

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
2

There is actually a significant difference.

Passing true forces the context of the returned jQuery object to be undefined.

$('.tag'), $('.tag', false), $('.tag', 'body') etc will all set the context to be document, the default.

I dont know why the original programmer have had this need for working on elements with no context, and do not dare to guess.

check it out -> console.dir($('.tag', true));

davidkonrad
  • 83,997
  • 17
  • 205
  • 265