NodeList Class
The NodeList class provides a wrapper for manipulating DOM NodeLists. NodeList properties can be accessed via the set/get methods. Use Y.all() to retrieve NodeList instances.
Constructor
Item Index
Methods
- addClass
- after
- append
- appendChild
- clearData
- concat
- destroy
- detach
- detachAll
- each
- empty
- even
- filter
- generateID
- get
- getAttribute
- getAttribute
- getComputedStyle
- getContent deprecated
- getData
- getDOMNodes
- getDOMNodes static
- getHTML
- getStyle
- hasClass
- hide
- indexOf
- insert
- insertBefore
- isEmpty
- item
- modulus
- odd
- on
- once
- onceAfter
- plug
- pop
- prepend
- push
- refresh
- remove
- removeAttribute
- removeClass
- replaceClass
- set
- setAttribute
- setAttribute
- setContent deprecated
- setData
- setHTML
- setStyle
- setStyles
- shift
- show
- size
- slice
- some
- splice
- toFrag
- toggleClass
- toggleView
- transition
- unplug
- unshift
- unwrap
- wrap
Properties
Methods
addClass
-
className
Adds a class name to each node.
Parameters:
-
className
Stringthe class name to add to the node's class attribute
after
-
type
-
fn
-
context
Applies an event listener to each Node bound to the NodeList. The handler is called only after all on() handlers are called and the event is not prevented.
Parameters:
Returns:
append
()
Called on each Node instance
appendChild
()
Called on each Node instance
clearData
-
name
Clears data on all Node instances bound to the NodeList.
Parameters:
-
name
StringThe name of the field to clear. If no name is given, all data is cleared.
concat
-
valueN
Parameters:
Returns:
destroy
-
recursivePurge
Called on each Node instance. Nulls internal node references, removes any plugins and event listeners
Parameters:
-
recursivePurge
Boolean(optional) Whether or not to remove listeners from the node's subtree (default is false)
detach
()
Called on each Node instance
detachAll
()
each
-
fn
-
context
Applies the given function to each Node in the NodeList.
empty
()
chainable
Called on each Node instance. Removes and destroys all of the nodes within the node
even
()
NodeList
Creates a new NodeList containing all nodes at even indices (zero-based index), including zero.
Returns:
filter
-
selector
Filters the NodeList instance down to only nodes matching the given selector.
Parameters:
-
selector
StringThe selector to filter against
Returns:
generateID
()
String
Applies a unique ID to each node if none exists
Returns:
get
()
getAttribute
-
name
Allows getting attributes on DOM nodes, normalizing in some cases. This passes through to the DOM node, allowing for custom attributes.
Parameters:
-
name
StringThe attribute name
Returns:
getAttribute
-
name
Allows getting attributes on DOM nodes, normalizing in some cases. This passes through to the DOM node, allowing for custom attributes.
Parameters:
-
name
StringThe attribute name
Returns:
getComputedStyle
-
attr
Returns an array of the computed value for each node. Use camelCase (e.g. 'backgroundColor') for multi-word properties.
Parameters:
-
attr
StringThe style attribute to retrieve.
Returns:
getContent
()
deprecated
Called on each Node instance
getData
-
name
Retrieves arbitrary data stored on each Node instance bound to the NodeList.
Parameters:
-
name
StringOptional name of the data field to retrieve. If no name is given, all data is returned.
Returns:
getDOMNodes
-
nodelist
Retrieves the DOM nodes bound to a NodeList instance
Parameters:
-
nodelist
NodeListThe NodeList instance
Returns:
getHTML
()
Called on each Node instance
getStyle
-
attr
Returns an array of values for each node. Use camelCase (e.g. 'backgroundColor') for multi-word properties.
Parameters:
-
attr
StringThe style attribute to retrieve.
Returns:
hasClass
-
className
Determines whether each node has the given className.
Parameters:
-
className
Stringthe class name to search for
Returns:
hide
-
name
-
config
-
callback
Hides each node. If the "transition" module is loaded, hide optionally animates the hiding of the node using either the default transition effect ('fadeOut'), or the given named effect.
indexOf
-
node
Returns the index of the node in the NodeList instance or -1 if the node isn't found.
Parameters:
-
node
Node | DOMNodethe node to search for
Returns:
insert
()
Called on each Node instance
insertBefore
()
Called on each Node instance
isEmpty
()
Boolean
Determines if the instance is bound to any nodes
Returns:
item
-
index
Retrieves the Node instance at the given index.
Parameters:
-
index
NumberThe index of the target Node.
Returns:
modulus
-
n
-
r
Creates a new NodeList containing all nodes at every n indices, where remainder n % index equals r. (zero-based index).
Parameters:
-
n
IntThe offset to use (return every nth node)
-
r
IntAn optional remainder to use with the modulus operation (defaults to zero)
Returns:
odd
()
NodeList
Creates a new NodeList containing all nodes at odd indices (zero-based index).
Returns:
on
-
type
-
fn
-
[context]
-
[arg*]
Subscribe a callback function for each Node
in the collection to execute
in response to a DOM event.
NOTE: Generally, the on()
method should be avoided on NodeLists
, in
favor of using event delegation from a parent Node. See the Event user
guide for details.
Most DOM events are associated with a preventable default behavior, such as
link clicks navigating to a new page. Callbacks are passed a
DOMEventFacade
object as their first argument (usually called e
) that
can be used to prevent this default behavior with e.preventDefault()
. See
the DOMEventFacade
API for all available properties and methods on the
object.
By default, the this
object will be the NodeList
that the subscription
came from, not the Node
that received the event. Use
e.currentTarget
to refer to the Node
.
Returning false
from a callback is supported as an alternative to calling
e.preventDefault(); e.stopPropagation();
. However, it is recommended to
use the event methods.
Parameters:
Returns:
Example:
Y.all(".sku").on("keydown", function (e) {
if (e.keyCode === 13) {
e.preventDefault();
// Use e.currentTarget to refer to the individual Node
var item = Y.MyApp.searchInventory( e.currentTarget.get('value') );
// etc ...
}
});
once
-
type
-
fn
-
context
Applies an one-time event listener to each Node bound to the NodeList.
Parameters:
Returns:
onceAfter
-
type
-
fn
-
context
Applies an one-time event listener to each Node bound to the NodeList that will be called only after all on() handlers are called and the event is not prevented.
Parameters:
Returns:
plug
-
P
-
config
Adds a plugin to each node in the NodeList. This will instantiate the plugin and attach it to the configured namespace on each node
Parameters:
-
P
Function | Object | ArrayAccepts the plugin class, or an object with a "fn" property specifying the plugin class and a "cfg" property specifying the configuration for the Plugin.
Additionally an Array can also be passed in, with the above function or object values, allowing the user to add multiple plugins in a single call.
-
config
Object(Optional) If the first argument is the plugin class, the second argument can be the configuration for the plugin.
prepend
()
Called on each Node instance
push
-
nodes
Parameters:
-
nodes
Node | DOMNodeOne or more nodes to add to the end of the NodeList.
refresh
()
chainable
Reruns the initial query, when created using a selector query
remove
-
destroy
Called on each Node instance. Removes the node from its parent. Shortcut for myNode.get('parentNode').removeChild(myNode);
Parameters:
-
destroy
Booleanwhether or not to call destroy() on the node after removal.
removeAttribute
-
name
Allows for removing attributes on DOM nodes. This passes through to the DOM node, allowing for custom attributes.
Parameters:
-
name
StringThe attribute to remove
removeClass
-
className
Removes a class name from each node.
Parameters:
-
className
Stringthe class name to remove from the node's class attribute
replaceClass
-
oldClassName
-
newClassName
Replace a class with another class for each node. If no oldClassName is present, the newClassName is simply added.
set
-
attr
-
val
Called on each Node instance. Sets an attribute on the Node instance. Unless pre-configured (via Node.ATTRS), set hands off to the underlying DOM node. Only valid attributes/properties for the node will be set. To set custom attributes use setAttribute.
Parameters:
-
attr
StringThe attribute to be set.
-
val
AnyThe value to set the attribute to.
setAttribute
-
name
-
value
Allows setting attributes on DOM nodes, normalizing in some cases. This passes through to the DOM node, allowing for custom attributes.
setAttribute
-
name
-
value
Allows setting attributes on DOM nodes, normalizing in some cases. This passes through to the DOM node, allowing for custom attributes.
setContent
()
deprecated
Called on each Node instance
Note that this passes to innerHTML and is not escaped.
Use Y.Escape.html()
to escape HTML, or set('text')
to add as text.
setData
-
name
-
val
Stores arbitrary data on each Node instance bound to the NodeList. This is not stored with the DOM node.
Parameters:
-
name
StringThe name of the field to set. If no name is given, name is treated as the data and overrides any existing data.
-
val
AnyThe value to be assigned to the field.
setHTML
()
Called on each Node instance
Note that this passes to innerHTML and is not escaped.
Use Y.Escape.html()
to escape HTML, or set('text')
to add as text.
setStyle
-
attr
-
val
Sets a style property on each node. Use camelCase (e.g. 'backgroundColor') for multi-word properties.
setStyles
-
hash
Sets multiple style properties on each node. Use camelCase (e.g. 'backgroundColor') for multi-word properties.
Parameters:
-
hash
ObjectAn object literal of property:value pairs.
show
-
name
-
config
-
callback
Makes each node visible. If the "transition" module is loaded, show optionally animates the showing of the node using either the default transition effect ('fadeIn'), or the given named effect.
size
()
Int
Returns the current number of items in the NodeList.
Returns:
slice
-
begin
-
end
Parameters:
-
begin
NumberZero-based index at which to begin extraction. As a negative index, start indicates an offset from the end of the sequence. slice(-2) extracts the second-to-last element and the last element in the sequence.
-
end
NumberZero-based index at which to end extraction. slice extracts up to but not including end. slice(1,4) extracts the second element through the fourth element (elements indexed 1, 2, and 3). As a negative index, end indicates an offset from the end of the sequence. slice(2,-1) extracts the third element through the second-to-last element in the sequence. If end is omitted, slice extracts to the end of the sequence.
Returns:
some
-
fn
-
context
Executes the function once for each node until a true value is returned.
Parameters:
Returns:
splice
-
index
-
howMany
Parameters:
-
index
NumberIndex at which to start changing the array. If negative, will begin that many elements from the end.
-
howMany
NumberAn integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element. If no howMany parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after index are removed. {Node | DOMNode| element1, ..., elementN The elements to add to the array. If you don't specify any elements, splice simply removes elements from the array.
Returns:
toFrag
()
Node
Creates a documenFragment from the nodes bound to the NodeList instance
Returns:
toggleClass
-
className
If the className exists on the node it is removed, if it doesn't exist it is added.
Parameters:
-
className
Stringthe class name to be toggled
toggleView
-
[on]
-
[callback]
Displays or hides each node. If the "transition" module is loaded, toggleView optionally animates the toggling of the nodes using either the default transition effect ('fadeIn'), or the given named effect.
transition
-
config
-
callback
Animate one or more css properties to a given value. Requires the "transition" module.
example usage: Y.all('.demo').transition({ duration: 1, // in seconds, default is 0.5 easing: 'ease-out', // default is 'ease' delay: '1', // delay start for 1 second, default is 0 height: '10px', width: '10px', opacity: { // per property value: 0, duration: 2, delay: 2, easing: 'ease-in' } });
unplug
-
plugin
Removes a plugin from all nodes in the NodeList. This will destroy the plugin instance and delete the namespace each node.
unwrap
()
chainable
Removes the parent node from node in the list.
wrap
-
html
Wraps the given HTML around each node.
Parameters:
-
html
StringThe markup to wrap around the node.
Properties
_nodes
Unknown
private
The underlying array of DOM nodes bound to the Y.NodeList instance