Simon Willison’s Weblog

Subscribe

Attribute selectors now supported

27th March 2003

I’ve updated document.getElementsBySelector to support CSS2 and CSS3 attribute selectors, as described here. Attribute selectors allow you to match elements based on any attribute using a variety or different rules including begins-with, ends-with, contains and more. The new version is tested in Mozilla, Phoenix and IE5/Windows (and I’m almost certain it works in IE6). There is a slight bug in Opera 7 preventing the ends-with selector from working which I have been unable to track down—any Opera javascript experts out there?

It has been pointed out that the function does not have the capability to deal with chained selectors such as div#main.layoutDiv. I have no intention of supporting this kind of advanced CSS syntax—one of my principle design aims is to keep the code short and simple, and implementing a full parser would add a lot of bulk and complexity without significantly improving the utility of the function.

This is Attribute selectors now supported by Simon Willison, posted on 27th March 2003.

Next: Why XML doesn't suck

Previous: Retrieving all DOM descendants

Previously hosted at http://simon.incutio.com/archive/2003/03/27/attributeSelectorsNowSupported