base2.DOM and the Selectors API

The Selectors API is a fairly simple specification as far as W3C specs go. It defines two methods for element retrieval. One method returns a single element, the other returns all matching elements. The only debate was what to call these methods. Finally, the debating is over, and in keeping with W3C tradition the editors have selected the longest names possible:

I like the new names really.:-)

I am going to rename the base2.DOM methods to stay in line with the standard. If you are using base2.DOM then this is a heads up.

I am also removing support for getElementsByClassName(). This is because I cannot support it fully. getElementsByClassName() is supposed to return a live node list. This is virtually impossible to replicate in JavaScript.

You can get identical functionality using the Selectors API:

var elements = document.getElementsByClassName("my-class");

is equivalent to:

var elements = document.querySelectorAll(".my-class");

NB: JavaScript implementations of getElementsByClassName() are notoriously slow so it is best to just avoid this selector entirely.

If you want to continue using getElementsByClassName() or the old Selectors API methods then include this function in your page:

(function() {

var OldNodeSelector = Interface.extend({
	"@!(element.getElementsByClassName)": {
		getElementsByClassName: function(node, className) {
			if (instanceOf(className, Array)) {
				className = className.join(".");
			return this.querySelectorAll(node, "." + className);
	matchAll: NodeSelector.querySelectorAll,
	matchSingle: NodeSelector.querySelector


If you are using the base2 core (you shouldn’t as it is not supported) then I have also renamed the Hash class and its methods to stay in line with the JS2 Map object which has very similar functionality.

These naming issues were preventing me from fully releasing base2.DOM, so expect a full release pretty soon. The code will be served from a permanent URL on Google code. By permanent, I mean that it will never be overwritten. The version number will form part of the URL somehow. I’m still undecided exactly how to do this as I also want the directory structure to mirror the base2 API.

Subscribe to the base2 Google group for further news about base2.

Update: It seems that the names have changed again! For clarification I emailed the editor of the Selectors API specification, Lachlan Hunt:

I got overruled! :-( We ended up holding a working group vote and querySelector/querySelectorAll won.

I’ve updated this post to reflect the latest names.

Comments (13)

Leave a comment

I’m utterly confused.

In fact, it was only last night that I was attempting to patch a local copy of Base2 so that I could start using get() and getAll()!

More seriously, though, the latest version in cvs

has querySelector() and querySelectorAll(). Nor is that a mistake

Revision 1.27 / (download) / (as text) - annotate - [select for diffs] , Tue Aug 21 05:40:25 2007 UTC (7 days, 15 hours ago) by lhunt
Changes since 1.26: +33 -33 lines
Diff to previous 1.26 (colored)

Changed method names to querySelector()/querySelectorAll() based on working group vote

Indeed. As chair, this is a formal announcement that the decision of the group is to use the name querySelector, and publish the last call draft with that name. (This gives the public a chance to raise any objection that they think will convince the group to open this debate and go round *AGAIN* – as W3C process requires – but means that within the group the issue is until then considered resolved by vote).


Mind you, back in June, Lachlan said that he was now prepared to go with cssQuery()

Mind you, back in June, Lachlan said that he was now prepared to go with cssQuery()…

I would have been flattered, but ultimately I think this is a bad name as it ties the Selectors API to CSS, which is not the greatest tool for this particular job.

  • Comment by: -dean
  • Posted:

Damn. Now I really need to buy that widescreen iMac. I hoped I could use my goold old 15″ iMac for another year;-)

  • Comment by: Doekman
  • Posted:

[…] UPDATE: Dean has actually implemented the Selectors API in his base2.DOM library and has taken out his getElementsByClassName implementation (as he can’t return a live node list). All you have to do is s/document.getElementsByClassName(”my-class”);/document.selectAllElements(”.my-class”);/ so to speak. […]

I just announced this result on my own blog.

Is this final? or is there any chance president Bush will overrule this voting;-)

Doeke, no, I suspect this is final. Note that originally there was a vote for getElementsBySelector, but there were a bunch of people that simply refused to use that name. That’s why they had to revote again, afaict.

  • Comment by: Martijn
  • Posted:

It’s pretty curious the debate between “Select” and “Choose”. I can agree that “Select” is more precise then “Choose”, but it doesn’t seem to be shorter nor longer.

Anyway the most important thing is to have a stable name.

  • Comment by: Alberto
  • Posted:

Nearly one year from the release of the draft, until the winners were announced, is a looong time.

  • Comment by: Thorsten
  • Posted:

Thank you for “querySelectorAll” attribute. I didn’t know about it:)

  • Comment by: Giftman
  • Posted:

I think ‘select’ is more precise, ‘choose’ is more on decision making.

  • Comment by: ben
  • Posted:

I think anne and Lachy made some good-faith decisions. Why the lowest score won the API naming?

[…] base2.DOM is now ready for a beta release. As previously discussed, I’ve renamed the Selectors API methods to keep in line with the standard. […]

Comments are closed.