3

jQuery currently uses window as its default element so any call like $('div') will look for div tags inside window.

Is there any way to change defaults on jQuery like:

$.defaultRoot = $('.anyOtherRootElement');
$('div').text("Hello");

this will select any div inside the elements containing .anyOtherRootElement class.

Thanks in advance


Upate

just an update refining the question a bit more here:

I would like to perform the actions above based on external queries coming from external script which won't know what defaultRoot is so they can still be calling what is supposed to be the current base, so in this instance, I'm afraid adding the a second parameter wouldn't be an option, unfortunately.

And at the same time creating a function which returns defaultRoot.find(el) would prevent me of using first-level methods such $.trim, $.each, etc… so unfortunately that would not be possible as well.

Community
  • 1
  • 1
zanona
  • 12,345
  • 25
  • 86
  • 141
  • why would the function prevent you from accessing methods? It's still returning a jQuery wrapper and the jQuery object would still be exposed? – Matt Whipple Sep 28 '12 at 11:46
  • hey Matt, I guess it's because it would be needed to run those under `$` and not `$s` or any other name – zanona Sep 28 '12 at 14:44
  • but $ would still be available for utility `$.(name)` functions and the return of $s would provide any wrapper `$.fn.(name)` functions. You could modify it so $s also allowed for utility functions, but in those cases jQuery is really just a namespace for "static"y methods so there's no real benefit but there would be far more complexity. – Matt Whipple Sep 28 '12 at 14:47
  • Oh I see what you mean, that makes much more sense now...thanks for your solution – zanona Sep 28 '12 at 14:49

4 Answers4

3

Ideally (for performance reasons) you'd want to use find()

$.defaultRoot.find("div");

Otherwise you can use the 2 argument form that sets a context

   $("div", $.defaultRoot);

In general you don't want to do these types of things implicitly since someone else could easily end up thoroughly confused when having to work with your code later. If you want to do it consistently and make it shorter you should create your own function to do so like:

var $s = function(selector) {
  return $.defaultRoot.find(selector);
}

and then you'd just be able to use

$s("div")

or you could also do a scoped higher order function with something like

var withScope = function(scope$) {
  return function(selector) {
    return scope$.find(selector);
  }
}

var $s = withScope($.defaultRoot);
$s("div")

If for some reason you really want to screw around with the default state for client code (begging for chaos IMO), you should look at the functional practice: currying.

Matt Whipple
  • 7,034
  • 1
  • 23
  • 34
0
$('SELECTOR', 'CONTEXT')

You can use context. As in your case $('div', '.anyOtherRootElement')

For more details, visit http://api.jquery.com/jQuery/

Riz
  • 9,703
  • 8
  • 38
  • 54
0

I don't think jQuery provide such method or variable. But you can pass second parameter in jQuery method to set context.

$.defaultRoot = $('.anyOtherRootElement');
$('div', $.defaultRoot ).text("Hello"); // all div inside $('.anyOtherRootElement')
$('div' ).text("Hello"); //all div inside body tag
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anoop
  • 23,044
  • 10
  • 62
  • 76
0

Given that you can pass the context as a second argument, you can easily overwrite the $() operator in Javascript with a version which internally calls JQuery using jQuery.noConflict(); and always passes your new root as the second argument.

Henrik Mühe
  • 419
  • 3
  • 24