YUI 3 introduced the Y.Node
constructor, which allows for the creation of a new Y.Node
instance by providing either a DOM element or selector string:
// Creates a Y.Node instance wrapping a div DOM element
var node = new Y.Node(document.createElement('div'));
However, it is recommended to use the more convenient Y.one
factory method:
// Creates a Y.Node instance wrapping a div DOM element
var node = Y.one(document.createElement('div'));
Additionally, YUI 3 offers a Y.NodeList
class to manage collections of Y.Node
instances:
// Returns a Y.NodeList representing all divs on the page
var divs = new Y.NodeList(document.getElementsByTagName('div'));
// Alternatively, using the Y.all NodeList factory method:
divs = Y.all(document.getElementsByTagName('div'));
// Lastly, the preferred method using a selector string:
divs = Y.all('div');
In general, utilize Y.one
and Y.all
to work with Y.Node
and Y.NodeList
instances respectively. This is the standard practice in YUI 3 development and is demonstrated in most examples.
If you need to remove a DOM element for which you already have a reference, you can achieve this using YUI 3’s Y.Node
class:
// Assumes el is a DOM element reference
Y.one(el).remove();