[whatwg] Proposal: Adding methods like getElementById and getElementsByTagName to DocumentFragments

Jussi Kalliokoski jussi.kalliokoski at gmail.com
Sun Jun 30 12:44:26 PDT 2013


On Sat, Jun 29, 2013 at 5:01 AM, Boris Zbarsky <bzbarsky at mit.edu> wrote:

> This is actually false.  For example, getElementById("foo:bar") is just
> querySelector("#foo\\:bar"), which is ... nonobvious.
>
> It gets worse if you don't control the id that's passed in, because
> getElementById(arg) becomes querySelector("#"+cssEscape(arg)) where
> cssEscape is a not entirely trivial-to-write function, if you want it to
> work reliably.


Not only is it not completely obvious how these methods are interoperable,
but also the readability of code involving querySelector is questionable:

this.buttonElement = document.querySelector('#' + this.buttonId);
this.buttonElement = document.getElementById(this.buttonId);

Not to mention that if you have to perform transformations on the variable,
such as .replace(/:/g, '//:'), in a lot of cases using querySelectors is
just way less clear a way of expressing the intention than the "obsolete"
methods that say perfectly well what you want. Query selectors are a very
powerful tool for complicated queries, but a lot of the time you don't need
that power and at least in those cases I'd prefer using a more expressive
way. The getElement methods aren't going away (and I think that's a good
thing) and I believe it's a good idea we be consistent here and make
DocumentFragments have these methods as well. Use the right tool for the
job.

Cheers,
Jussi



More information about the whatwg mailing list