5
<title>Example: Using NodeList - Simple</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Using NodeList - Simple</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><link href="../assets/node/node.css" rel="stylesheet" type="text/css">
24
<p>This example demonstrates how to use a NodeList instance to make it easy to manipulate multiple nodes at once.</p>
30
display: inline-block;
31
zoom: 1; *display: inline; /* IE < 8: fake inline-block */
35
margin: 0.5em 1em 0.5em 0;
36
border: solid 1px #ccc;
37
background-color: #F4E6B8;
52
YUI().use('node', function(Y){
53
var boxes = Y.all('.box-row li');
55
var handleBoxClick = function(e) {
56
// boxes is a NodeList
57
boxes.setContent('neener');
58
boxes.setStyle('backgroundColor', '#F4E6B8');
60
// e.currentTarget === .box-row li, just the one that was clicked
61
e.currentTarget.setContent('ouch!');
62
e.currentTarget.setStyle('backgroundColor', '#C4DAED');
64
Y.one('.box-row').delegate('click', handleBoxClick, 'li');
70
<h2>Setting up the Node</h2>
71
<p>First we need some HTML to work with.</p>
72
<pre class="code prettyprint">Click us.
73
<ul class="box-row">
74
<li>box</li>
75
<li>box</li>
76
<li>box</li>
77
<li>box</li>
78
<li>box</li>
79
</ul></pre>
82
<p>CSS to make boxes in horizontal row</p>
83
<pre class="code prettyprint"><style>
85
display: inline-block;
86
zoom: 1; *display: inline; /* IE < 8: fake inline-block */
90
margin: 0.5em 1em 0.5em 0;
91
border: solid 1px #ccc;
92
background-color: #F4E6B8;
95
</style></pre>
98
<h2>Geting a NodeList Instance</h2>
99
<p>We will use the <code>all</code> method of our YUI instance to get a NodeList instance to work with.</p>
100
<pre class="code prettyprint">var boxes = Y.all('.box-row li');</pre>
103
<h2>Accessing NodeList Properties</h2>
104
<p>As with Node, simple type of properties (strings, numbers, booleans)
105
pass directly to/from the underlying HTMLElement, however properties
106
representing HTMLElements return Node instances.</p>
107
<p>In this example, we will listen for a <code>click</code> event to trigger the property change.</p>
108
<p>Note that the context of the handler is set to the NodeList, so <code>this</code>
109
refers to our NodeList instance. The <code>currentTarget</code> property of
110
the event object is set to the current Node instance.</p>
111
<pre class="code prettyprint">var handleBoxClick = function(e) {
112
// this === boxes, which is a NodeList
113
this.setContent('neener');
114
this.setStyle('backgroundColor', '#F4E6B8');
116
// e.currentTarget === .box-row li, just the one that was clicked
117
e.currentTarget.setContent('ouch!');
118
e.currentTarget.setStyle('backgroundColor', '#fff');
120
boxes.on('click', handleBoxClick);</pre>
123
<h2>Prefer <code>node.delegate()</code> over <code>nodelist.on()</code></h2>
125
<p>Sometimes you need to create individual subscriptions for each Node in a
126
NodeList (as is done in this example), but usually it's preferable to use
127
<a href="node-evt-delegation.html">event delegation</a>.</p>
129
<h2>Complete Simple Box Example Source</h2>
130
<pre class="code prettyprint"><style>
131
.example .box-row li{
132
display: inline-block;
133
zoom: 1; *display: inline; /* IE < 8: fake inline-block */
137
margin: 0.5em 1em 0.5em 0;
138
border: solid 1px #ccc;
139
background-color: #F4E6B8;
145
<ul class="box-row">
146
<li>box</li>
147
<li>box</li>
148
<li>box</li>
149
<li>box</li>
150
<li>box</li>
154
YUI().use('node', function(Y){
155
var boxes = Y.all('.box-row li');
157
var handleBoxClick = function(e) {
158
// boxes is a NodeList
159
boxes.setContent('neener');
160
boxes.setStyle('backgroundColor', '#F4E6B8');
162
// e.currentTarget === .box-row li, just the one that was clicked
163
e.currentTarget.setContent('ouch!');
164
e.currentTarget.setStyle('backgroundColor', '#C4DAED');
166
Y.one('.box-row').delegate('click', handleBoxClick, 'li');
168
</script></pre>
174
<div class="yui3-u-1-4">
175
<div class="sidebar">
179
<div class="sidebox">
181
<h2 class="no-toc">Examples</h2>
185
<ul class="examples">
188
<li data-description="Using selectors and property accessors with Node.">
189
<a href="properties.html">Set and Get Properties</a>
194
<li data-description="Using DOM methods with Node.">
195
<a href="dom-node.html">DOM Methods</a>
200
<li data-description="Listening for DOM events with Node instances.">
201
<a href="events.html">Handling DOM Events</a>
206
<li data-description="NodeList provides Node functionality for manipulating multiple nodes at once.">
207
<a href="nodelist.html">Using NodeList - Simple</a>
212
<li data-description="How to use multiple NodeList features to build a simple game.">
213
<a href="ducks.html">Using NodeList - Ducks Game</a>
218
<li data-description="Using a single event listener to handle events on multiple nodes.">
219
<a href="node-evt-delegation.html">Delegating Node Events</a>
224
<li data-description="This example demonstrates how to position an element in page coordinates.">
225
<a href="node-xy.html">Node Positioning</a>
230
<li data-description="This example demonstrates how to set styles and get style information.">
231
<a href="node-style.html">Node Styling</a>
236
<li data-description="This example demonstrates how to insert content into a Node.">
237
<a href="node-insert.html">Adding Node Content</a>
242
<li data-description="This example demonstrates how to show and hide a Node.">
243
<a href="node-view.html">Showing and Hiding</a>
267
<div class="sidebox">
269
<h2 class="no-toc">Examples That Use This Component</h2>
273
<ul class="examples">
296
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States.">
297
<a href="../node-focusmanager/node-focusmanager-1.html">Accessible Toolbar</a>
302
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States.">
303
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
308
<li data-description="Use the Event Utility to attach simple DOM event handlers.">
309
<a href="../event/basic-example.html">Simple DOM Events</a>
314
<li data-description="Use IO to request XML data from a remote web service.">
315
<a href="../io/weather.html">Request XML data from Yahoo! Weather</a>
320
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
321
<a href="../io/xdr.html">Request JSON using Yahoo! Pipes</a>
326
<li data-description="Example Photo Browser application.">
327
<a href="../dd/photo-browser.html">Photo Browser</a>
332
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
333
<a href="../dd/portal-drag.html">Portal Style Example</a>
346
<script src="../assets/vendor/prettify/prettify-min.js"></script>
347
<script>prettyPrint();</script>