Hi,
> $('som_div')
> $('som_div div.child_of_some_div')
>
> I find myself frequently referencing child divs so use .down() a lot. It
> would be convenient at times to get to that child div directly with the $
> shortcut as I've exampled above.
If it's just syntactic sugar you're looking for, you can readily
create your own function for that:
function $X(id_like_thing) {
var index,
childSelector,
element;
index = id_like_thing.indexOf(" ");
if (index >= 0) {
childSelector = id_like_thing.substring(index+1).strip();
id_like_thing = id_like_thing.substring(0, index - 1);
}
element = $(id_like_thing);
if (element && childSelector) {
element = element.down(childSelector);
}
return element;
}
(Untested off-the-cuff code.)
> Is there another convenient means to accomplish this in the class or
> am I stuck doing this:
> var _myClass = new myClass($('my_div').down('div.child'))
If you're trying to avoid `down` for some reason, you can do this:
var _myClass = new myClass($$('#my_div div.child')[0])
The more specific you can be on the right-hand side of that, the
better, because the way most selector engines work is right-to-left:
First find matches for `div.child` and then work up the ancestor chain
of each match to see if the rest of the selector matches. Of course,
you'd have to profile that to see whether it's slower or faster (if
you care) than `down`. Be sure to profile on your target browsers, as
the answer will vary markedly depending on whether the browser
supports `querySelectorAll`.
FWIW,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com