5
<title>Example: Handling DOM Events</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Handling DOM Events</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
21
<p>This example demonstrates how to respond to DOM events from a Node instance.</p>
22
<p>Clicking the box will report some event details.</p>
26
<link href="../assets/node/node.css" rel="stylesheet" type="text/css">
28
<p><em>lorem</em> <code>ipsum</code> <strong>dolor</strong> <span>sit</span></p>
29
<div id="event-result">Click anywhere in the box to see some event data.</div>
32
<script type="text/javascript">
33
YUI().use('node', function(Y) {
34
var onClick = function(e) {
36
currentTarget = e.currentTarget, // #demo
37
target = e.target; // #demo or a descendant
39
Y.one('#event-result').setContent('<dl>' +
40
'<dt>type</dt><dd>' + e.type + '</dd>' +
41
'<dt>target</dt><dd>' + target.get('tagName') + '</dd>' +
42
'<dt>currentTarget</dt><dd>' + currentTarget.get('tagName') +
43
'#' + currentTarget.get('id') + '</dd></dl>');
46
Y.one('#demo').on('click', onClick);
53
<h2>Setting up the HTML</h2>
54
<p>First we need some HTML to work with.</p>
55
<pre class="code prettyprint"><div id="demo">
56
<p><em>lorem</em> <code>ipsum</code> <strong>dolor</strong> <span>sit</span></p>
57
<div id="event-result"></div>
58
</div></pre>
60
<h2>Creating the Event Handler</h2>
61
<p>Next we will create a handler to run when the event is fired. In our handler we will update the node with some of the properties of the event.</p>
62
<pre class="code prettyprint">var onClick = function(e) {
64
currentTarget = e.currentTarget, // #demo
65
target = e.target; // #demo or a descendant
67
Y.one('#event-result').setContent('<dl>' +
68
'<dt>type</dt><dd>' + e.type + '</dd>' +
69
'<dt>target</dt><dd>' + target.get('tagName') + '</dd>' +
70
'<dt>currentTarget</dt><dd>' + currentTarget.get('tagName') +
71
'#' + currentTarget.get('id') + '</dd></dl>');
75
<h2>Listening for Events</h2>
76
<p>We can assign our handler to all of the items by using the <code>all</code> method to get a NodeList instance and using the <code>on</code> method to subscribe to the event.</p>
77
<pre class="code prettyprint">Y.one('#demo').on('click', onClick);</pre>
80
<h2>Complete Example Source</h2>
81
<pre class="code prettyprint"><link href="../assets/node/node.css" rel="stylesheet" type="text/css">
82
<div id="demo">
83
<p><em>lorem</em> <code>ipsum</code> <strong>dolor</strong> <span>sit</span></p>
84
<div id="event-result">Click anywhere in the box to see some event data.</div>
87
<script type="text/javascript">
88
YUI().use('node', function(Y) {
89
var onClick = function(e) {
91
currentTarget = e.currentTarget, // #demo
92
target = e.target; // #demo or a descendant
94
Y.one('#event-result').setContent('<dl>' +
95
'<dt>type</dt><dd>' + e.type + '</dd>' +
96
'<dt>target</dt><dd>' + target.get('tagName') + '</dd>' +
97
'<dt>currentTarget</dt><dd>' + currentTarget.get('tagName') +
98
'#' + currentTarget.get('id') + '</dd></dl>');
101
Y.one('#demo').on('click', onClick);
103
</script></pre>
108
<div id="sidebar" class="yui3-u">
112
<div class="sidebox">
114
<h2 class="no-toc">Examples</h2>
118
<ul class="examples">
121
<li data-description="Using selectors and property accessors with Node.">
122
<a href="properties.html">Set and Get Properties</a>
127
<li data-description="Using DOM methods with Node.">
128
<a href="dom-node.html">DOM Methods</a>
133
<li data-description="Listening for DOM events with Node instances.">
134
<a href="events.html">Handling DOM Events</a>
139
<li data-description="NodeList provides Node functionality for multiple nodes.">
140
<a href="nodelist.html">NodeList</a>
145
<li data-description="Using a single event listener to handle events on multiple nodes.">
146
<a href="node-evt-delegation.html">Delegating Node Events</a>
151
<li data-description="This example demonstrates how to position an element in page coordinates.">
152
<a href="node-xy.html">Node Positioning</a>
157
<li data-description="This example demonstrates how to set styles and get style information.">
158
<a href="node-style.html">Node Styling</a>
163
<li data-description="This example demonstrates how to insert content into a Node.">
164
<a href="node-insert.html">Adding Node Content</a>
169
<li data-description="This example demonstrates how to show and hide a Node.">
170
<a href="node-view.html">Showing and Hiding</a>
194
<div class="sidebox">
196
<h2 class="no-toc">Examples That Use This Component</h2>
200
<ul class="examples">
221
<li data-description="Use the Event Utility to attach simple DOM event handlers.">
222
<a href="../event/basic-example.html">Simple DOM Events</a>
227
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States.">
228
<a href="../node-focusmanager/node-focusmanager-1.html">Accessible Toolbar</a>
233
<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.">
234
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
239
<li data-description="Use IO to request XML data from a remote web service.">
240
<a href="../io/weather.html">Request XML data from Yahoo! Weather</a>
245
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
246
<a href="../io/xdr.html">Request JSON using Yahoo! Pipes</a>
251
<li data-description="Example Photo Browser application.">
252
<a href="../dd/photo-browser.html">Photo Browser</a>
257
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
258
<a href="../dd/portal-drag.html">Portal Style Example</a>
270
<script src="../assets/vendor/prettify/prettify-min.js"></script>
271
<script>prettyPrint();</script>