5
<title>Example: Delegating Node Events</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: Delegating Node Events</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 using a single event listener on a list to delegate clicks on the list items.</p>
29
<li><em>click me if you don't mind...</em></li>
30
<li><em>click me if you don't mind...</em></li>
31
<li><em>click me if you don't mind...</em></li>
32
<li><em>click me if you don't mind...</em></li>
35
<script type="text/javascript">
36
YUI().use('node', function(Y) {
37
var nodes = Y.all('#demo li');
39
var onClick = function(e) {
40
e.currentTarget.addClass('highlight'); // e.currentTarget === #demo li
41
e.target.setContent('thanks for the click!'); // e.target === #demo li or #demo li em
42
e.container.setStyle('border', '5px solid blue'); // e.container === #demo
44
nodes.filter(':not(.highlight)').setContent('Click me too please!');
47
Y.one('#demo').delegate('click', onClick, 'li');
53
<h2>Setting up the HTML</h2>
54
<p>First we need some HTML to work with.</p>
55
<pre class="code prettyprint"><ul id="demo">
56
<li>click me if you don't mind...</li>
57
<li>click me if you don't mind...</li>
58
<li>click me if you don't mind...</li>
59
<li>click me if you don't mind...</li>
60
</ul></pre>
63
<h2>Geting a NodeList Instance</h2>
64
<p>We will use the <code>all</code> method of our YUI instance to get a <code>NodeList</code> instance to work with.</p>
65
<pre class="code prettyprint">var nodes = Y.all('#demo li');</pre>
68
<h2>Delegating Node Events</h2>
69
<p>In this example, we will listen for a <code>click</code> event on the list and handle it at the item level and use information from the <code>event</code> object to update the nodes.</p>
70
<pre class="code prettyprint">var onClick = function(e) {
71
e.currentTarget.addClass('yui-pass'); // e.currentTarget === #demo li
72
e.target.setContent('thanks for the click!'); // e.target === #demo li or #demo li em
73
e.container.setStyle('border', '5px solid blue'); // e.container === #demo
75
nodes.filter(':not(.yui3-pass)').setContent('Click me too please!');
79
<p>Now we just assign the handler to the <code>UL</code> using the <code>delegate</code> method to handle clicks on each <code>LI</code>.</p>
80
<pre class="code prettyprint">Y.one('#demo').delegate('click', onClick, 'li');</pre>
83
<h2>Complete Example Source</h2>
84
<pre class="code prettyprint"><ul id="demo">
85
<li><em>click me if you don't mind...</em></li>
86
<li><em>click me if you don't mind...</em></li>
87
<li><em>click me if you don't mind...</em></li>
88
<li><em>click me if you don't mind...</em></li>
91
<script type="text/javascript">
92
YUI().use('node', function(Y) {
93
var nodes = Y.all('#demo li');
95
var onClick = function(e) {
96
e.currentTarget.addClass('highlight'); // e.currentTarget === #demo li
97
e.target.setContent('thanks for the click!'); // e.target === #demo li or #demo li em
98
e.container.setStyle('border', '5px solid blue'); // e.container === #demo
100
nodes.filter(':not(.highlight)').setContent('Click me too please!');
103
Y.one('#demo').delegate('click', onClick, 'li');
105
</script></pre>
111
<div class="yui3-u-1-4">
112
<div class="sidebar">
116
<div class="sidebox">
118
<h2 class="no-toc">Examples</h2>
122
<ul class="examples">
125
<li data-description="Using selectors and property accessors with Node.">
126
<a href="properties.html">Set and Get Properties</a>
131
<li data-description="Using DOM methods with Node.">
132
<a href="dom-node.html">DOM Methods</a>
137
<li data-description="Listening for DOM events with Node instances.">
138
<a href="events.html">Handling DOM Events</a>
143
<li data-description="NodeList provides Node functionality for manipulating multiple nodes at once.">
144
<a href="nodelist.html">Using NodeList - Simple</a>
149
<li data-description="How to use multiple NodeList features to build a simple game.">
150
<a href="ducks.html">Using NodeList - Ducks Game</a>
155
<li data-description="Using a single event listener to handle events on multiple nodes.">
156
<a href="node-evt-delegation.html">Delegating Node Events</a>
161
<li data-description="This example demonstrates how to position an element in page coordinates.">
162
<a href="node-xy.html">Node Positioning</a>
167
<li data-description="This example demonstrates how to set styles and get style information.">
168
<a href="node-style.html">Node Styling</a>
173
<li data-description="This example demonstrates how to insert content into a Node.">
174
<a href="node-insert.html">Adding Node Content</a>
179
<li data-description="This example demonstrates how to show and hide a Node.">
180
<a href="node-view.html">Showing and Hiding</a>
204
<div class="sidebox">
206
<h2 class="no-toc">Examples That Use This Component</h2>
210
<ul class="examples">
233
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States.">
234
<a href="../node-focusmanager/node-focusmanager-1.html">Accessible Toolbar</a>
239
<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.">
240
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
245
<li data-description="Use the Event Utility to attach simple DOM event handlers.">
246
<a href="../event/basic-example.html">Simple DOM Events</a>
251
<li data-description="Use IO to request XML data from a remote web service.">
252
<a href="../io/weather.html">Request XML data from Yahoo! Weather</a>
257
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
258
<a href="../io/xdr.html">Request JSON using Yahoo! Pipes</a>
263
<li data-description="Example Photo Browser application.">
264
<a href="../dd/photo-browser.html">Photo Browser</a>
269
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
270
<a href="../dd/portal-drag.html">Portal Style Example</a>
283
<script src="../assets/vendor/prettify/prettify-min.js"></script>
284
<script>prettyPrint();</script>