YUI recommends YUI3.
YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.
This documentation is no longer maintained.
The YUI Selector Utility brings CSS3 Selector syntax to JavaScript, providing a compact shorthand for collecting, filtering, and testing HTMLElements.
To use the Selector Utility, include the following source files in your web page with the <script>
tag:
<!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script> <!-- Source file --> <script src="http://yui.yahooapis.com/2.9.0/build/selector/selector-min.js"></script>
<!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script> <!-- Source file --> <script src="http://yui.yahooapis.com/2.9.0/build/selector/selector-min.js"></script>
Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader to write out the full HTML for including the precise files you need for your implementation.
Note: If you wish to include this component via the YUI Loader, its module name is selector
. (Click here for the full list of module names for YUI Loader.)
Where these files come from: The files included using the text above will be served from Yahoo! servers. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug
versions of YUI JavaScript files, please download the library distribution and host the files on your own server.
Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.
The Selector API is most commonly used to retrieve a collection of elements that match a given CSS selector. This is done using the query()
method.
var nodes = YAHOO.util.Selector.query('div p');
var nodes = YAHOO.util.Selector.query('div p');
In this example, the array of nodes returned contains all HTMLElements that are of type P
that have a DIV
ancestor.
The query()
method can take an optional root node from which to begin the search and a boolean third argument that, if true
, will cause the Selector Utility to return only the first node that matches the selector. For example, the following query will return only the first P element with a DIV ancestor that is a descendent of the element with an ID attribute of foo
:
var node = YAHOO.util.Selector.query('div p', 'foo', true);
var node = YAHOO.util.Selector.query('div p', 'foo', true);
This section describes several common uses of Selector. It contains these subsections:
The query
method can be useful for attaching events to a number of elements.
var handleClick = function(e) { alert('click'); }; var nodes = YAHOO.util.Selector.query('li a'); YAHOO.util.Event.on(nodes, 'click', handleClick);
var handleClick = function(e) { alert('click'); }; var nodes = YAHOO.util.Selector.query('li a'); YAHOO.util.Event.on(nodes, 'click', handleClick);
The above example listens for clicks on all anchors that are descendants of an LI
element. The handleClick
function is invoked when any of of the nodes are clicked.
Another useful method that Selector provides is test()
, which accepts a node and a simple selector, and returns true
or false
, depending on whether or not the node matches the provided selector.
var node = document.getElementById('foo'); alert(YAHOO.util.Selector.test(node, 'div.bar'));
var node = document.getElementById('foo'); alert(YAHOO.util.Selector.test(node, 'div.bar'));
The above example will alert true
if the node is of type DIV
and has a class of bar
, otherwise it will alert false
.
Selector also provides a means for filtering a set of nodes based on a simple selector.
var candidates = document.getElementsByTagName('a'); var nodes = YAHOO.util.Selector.filter(candidates, '[href]');
var candidates = document.getElementsByTagName('a'); var nodes = YAHOO.util.Selector.filter(candidates, '[href]');
The above example filters the candidate anchor elements and returns only those that have the href
attribute set.
Selector implements CSS3 Attribute Selectors, which includes a number of powerful operators for use in testing attributes.
For example, say you want to collect all input elements whose names begin with "yui". This is a job for the "starts with" ("^=") operator:
var nodes = YAHOO.util.Selector.query('input[name^=yui]');
var nodes = YAHOO.util.Selector.query('input[name^=yui]');
The above example returns only input elements whose name
attribute begins with "yui". See the CSS3 Specification for additional examples of attribute operators.
Selector also implements most CSS3 Pseudo-classes, allowing for additional matching based on information that can not be expressed using simple selectors.
For example, you may want only the first child item of an unordered list:
var nodes = YAHOO.util.Selector.query('ul li:first-child');
var nodes = YAHOO.util.Selector.query('ul li:first-child');
The not
pseudo-class can be used if you want to match elements that DO NOT meet certain criteria:
var nodes = YAHOO.util.Selector.query('input:not([type=button])');
var nodes = YAHOO.util.Selector.query('input:not([type=button])');
The above example returns only input elements that are NOT of type BUTTON. See the CSS3 Specification for additional examples of pseudo-classes.
About this Section: YUI generally works well with mobile browsers that are based on A-Grade browser foundations. For example, Nokia's N-series phones, including the N95, use a browser based on Webkit — the same foundation shared by Apple's Safari browser, which is found on the iPhone. The fundamental challenges in developing for this emerging class of full, A-Grade-derived browsers on handheld devices are:
There are other considerations, many of them device/browser specific (for example, current versions of the iPhone's Safari browser do not support Flash). The goal of these sections on YUI User's Guides is to provide you some preliminary insights about how specific components perform on this emerging class of mobile devices. Although we have not done exhaustive testing, and although these browsers are revving quickly and present a moving target, our goal is to provide some early, provisional advice to help you get started as you contemplate how your YUI-based application will render in the mobile world.
More Information:
The Selector Utility is fully supported on Apple's iPhone and the Nokia N-Series browsers; you can expect functional parity between Selector Utility on these devices as compared to the supported A-Grade browsers.
The YUI Library and related topics are discussed on the on the YUILibrary.com forums.
Also be sure to check out YUIBlog for updates and articles about the YUI Library written by the library's developers.
The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.
be the first to bookmark this page!
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.
Copyright © 2013 Yahoo! Inc. All rights reserved.