W3C forges ahead with Selectors API
The Web Applications Working Group at the W3C has published a last call working draft of the Selectors API Level 1 specification. Widely used in CSS, selectors are patterns that match a set of elements in a structure tree. As accessing elements in HTML documents using DOM methods such as
getElementsByTagName can quite laborious, frameworks like jQuery have developed simple CSS selector methods. Many browsers offer
querySelectorAll functions that also use these selectors.
querySelectorAll selectors allow DOM elements to be selected in a similar way to how they are selected in Cascading Style Sheets (CSS). For example,
querySelectorAll('p.warning, p.error) will return all
P elements that contain the "warning" or "error"
LI elements in an unordered list whose ID is "Navigation" can be returned using
querySelectorAll('ul#Navigation>li'). Any issues can be raised until 19 July 2012, after which time the document will become a "recommendation" and will reach standard status.
The simultaneously released working draft of Level 2 of the Selectors API also contains the
matches methods. The first two functions search for the required elements in a set of DOM elements that is submitted as a parameter and return any elements they find. The
matches method, on the other hand, only checks whether one of the scanned elements matches the selector.