W3C home > Mailing lists > Public > whatwg@whatwg.org > June 2013

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

From: Jussi Kalliokoski <jussi.kalliokoski@gmail.com>
Date: Sun, 30 Jun 2013 22:44:26 +0300
Message-ID: <CAJhzemUrnkvx-2iTNpMEmVBORY5ku29SY4ZQtXzDDpnXoZFy1w@mail.gmail.com>
To: Boris Zbarsky <bzbarsky@mit.edu>
Cc: whatwg@lists.whatwg.org
On Sat, Jun 29, 2013 at 5:01 AM, Boris Zbarsky <bzbarsky@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

Received on Sunday, 30 June 2013 19:44:50 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 17:00:00 UTC