<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example: Handling DOM Events</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<h1>Example: Handling DOM Events</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><div class="intro">
<p>This example demonstrates how to respond to DOM events from a Node instance.</p>
<p>Clicking one of the elements will report some event details.</p>
</div>
<div class="example">
<style>
#container{
font-size: 200%;
cursor: pointer;
padding: 0 0.5em;
margin-bottom: 0.3em;
border-bottom: solid 1px #ccc;
text-align: center;
}
#container em{
color: red;
font-weight: bold;
font-size: 130%;
}
#container strong{
color: green;
font-weight: bold;
font-family: arial black;
}
#container code{
background-color: #000;
color: #CEFFA2;
padding: 0.3em;
font-weight: bold;
font-family: Courier,monospace;
}
#container a{
color: #00f;
padding: 0.3em;
text-decoration: underline;
font-family: verdana;
}
.example .dd-color{
height: 1em;
width: 3em;
}
.example dt{
font-weight: normal;
color: #999999;
}
.example dd{
margin: 0 1.5em 0.3em;
}
.example dl{
margin: 0;
}
</style>
<div id="container">
<p> <em>emphasis</em> <code>code</code> <strong>strong</strong> <a>anchor</a> <img src="../assets/node/images/birds.png" align="middle"/></p>
</div>
<div id="event-result">Click an element above to see its event data.</div>
<script type="text/javascript">
YUI().use('node', function(Y) {
var onClick = function(e) {
var type = e.type,
currentTarget = e.currentTarget, // #container
target = e.target; // #container or a descendant
Y.one('#event-result').setHTML('<dl>' +
'<dt>Event Type: </dt>' +
'<dd>' + e.type + '</dd>' +
'<dt>Target Tag Name: </dt>' +
'<dd>' + target.get('tagName') + '</dd>' +
'<dt>Color of Target\'s Font: </dt>' +
'<div class="dd-color" style="background-color:' + target.getComputedStyle('color') + ';">' + '</div>' +
'<dt>CurrentTarget Tag Name & Id: </dt>' +
'<dd>' + currentTarget.get('tagName') + '#' + currentTarget.get('id') + '</dd>' +
'</dl>');
};
Y.one('#container').on('click', onClick);
Y.one('#container').on('dblclick', onClick);
});
</script>
</div>
<h2>Setting up the HTML</h2>
<p>First we need some HTML to work with.</p>
<pre class="code prettyprint"><div id="container">
<p>
<em>emphasis</em> <code>code</code> <strong>strong</strong> <a>anchor</a>
<img src="../assets/node/images/birds.png" align="middle"/>
</p>
</div>
<div id="event-result">Click an element above to see its event data.</div></pre>
<h2>Creating the Event Handler</h2>
<p>Next we'll create a handler to run when the event is fired. In our handler
we'll update the <code>#event-result</code> node with some data available through the event.</p>
<pre class="code prettyprint">var onClick = function(e) {
var type = e.type,
currentTarget = e.currentTarget, // #container
target = e.target; // #container or a descendant
Y.one('#event-result').setHTML('<dl>' +
'<dt>Event Type: </dt>' +
'<dd>' + e.type + '</dd>' +
'<dt>Target Tag Name: </dt>' +
'<dd>' + target.get('tagName') + '</dd>' +
'<dt>Color of Target's Font: </dt>' +
'<dd class="dd-color" style="background-color:' + target.getStyle('color') + ';">' + '</dd>' +
'<dt>CurrentTarget Tag Name & Id: </dt>' +
'<dd>' + currentTarget.get('tagName') + '#' + currentTarget.get('id') + '</dd>' +
'</dl>');
};</pre>
<h2>Listening for Events</h2>
<p>We can assign our handler to the container of the objects by using the <code>Y.one</code>.
Clicking on any object in the container will bubble the event to the container.
We're using the 'on' method to subscribe to the click and dblclick events.</p>
<pre class="code prettyprint">Y.one('#container').on('click', onClick);
Y.one('#container').on('dblclick', onClick);</pre>
<h2>Complete Example Source</h2>
<pre class="code prettyprint"><style>
#container{
font-size: 200%;
cursor: pointer;
padding: 0 0.5em;
margin-bottom: 0.3em;
border-bottom: solid 1px #ccc;
text-align: center;
}
#container em{
color: red;
font-weight: bold;
font-size: 130%;
}
#container strong{
color: green;
font-weight: bold;
font-family: arial black;
}
#container code{
background-color: #000;
color: #CEFFA2;
padding: 0.3em;
font-weight: bold;
font-family: Courier,monospace;
}
#container a{
color: #00f;
padding: 0.3em;
text-decoration: underline;
font-family: verdana;
}
.example .dd-color{
height: 1em;
width: 3em;
}
.example dt{
font-weight: normal;
color: #999999;
}
.example dd{
margin: 0 1.5em 0.3em;
}
.example dl{
margin: 0;
}
</style>
<div id="container">
<p> <em>emphasis</em> <code>code</code> <strong>strong</strong> <a>anchor</a> <img src="../assets/node/images/birds.png" align="middle"/></p>
</div>
<div id="event-result">Click an element above to see its event data.</div>
<script type="text/javascript">
YUI().use('node', function(Y) {
var onClick = function(e) {
var type = e.type,
currentTarget = e.currentTarget, // #container
target = e.target; // #container or a descendant
Y.one('#event-result').setHTML('<dl>' +
'<dt>Event Type: </dt>' +
'<dd>' + e.type + '</dd>' +
'<dt>Target Tag Name: </dt>' +
'<dd>' + target.get('tagName') + '</dd>' +
'<dt>Color of Target\'s Font: </dt>' +
'<div class="dd-color" style="background-color:' + target.getComputedStyle('color') + ';">' + '</div>' +
'<dt>CurrentTarget Tag Name & Id: </dt>' +
'<dd>' + currentTarget.get('tagName') + '#' + currentTarget.get('id') + '</dd>' +
'</dl>');
};
Y.one('#container').on('click', onClick);
Y.one('#container').on('dblclick', onClick);
});
</script></pre>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Using selectors and property accessors with Node.">
<a href="properties.html">Set and Get Properties</a>
</li>
<li data-description="Using DOM methods with Node.">
<a href="dom-node.html">DOM Methods</a>
</li>
<li data-description="Building a simple store and shopping cart.">
<a href="store.html">DOM Methods - Store</a>
</li>
<li data-description="Listening for DOM events with Node instances.">
<a href="events.html">Handling DOM Events</a>
</li>
<li data-description="NodeList provides Node functionality for manipulating multiple nodes at once.">
<a href="nodelist.html">Using NodeList - Simple</a>
</li>
<li data-description="How to use multiple NodeList features to build a simple game.">
<a href="ducks.html">Using NodeList - Ducks Game</a>
</li>
<li data-description="Using a single event listener to handle events on multiple nodes.">
<a href="node-evt-delegation.html">Delegating Node Events</a>
</li>
<li data-description="This example demonstrates how to position an element in page coordinates.">
<a href="node-xy.html">Node Positioning</a>
</li>
<li data-description="This example demonstrates how to set styles and get style information.">
<a href="node-style.html">Node Styling</a>
</li>
<li data-description="This example demonstrates how to insert content into a Node.">
<a href="node-insert.html">Adding Node Content - Burger Builder</a>
</li>
<li data-description="This example demonstrates how to show and hide a Node.">
<a href="node-view.html">Showing and Hiding</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples That Use This Component</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States.">
<a href="../node-focusmanager/node-focusmanager-toolbar.html">Accessible Toolbar</a>
</li>
<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.">
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a>
</li>
<li data-description="Use the Event Utility to attach simple DOM event handlers.">
<a href="../event/basic-example.html">Simple DOM Events</a>
</li>
<li data-description="Example Photo Browser application.">
<a href="../dd/photo-browser.html">Photo Browser</a>
</li>
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
<a href="../dd/portal-drag.html">Portal Style Example</a>
</li>
<li data-description="Use IO to request XML data from a remote web service.">
<a href="../io/weather.html">Request XML data from Yahoo! Weather</a>
</li>
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
<a href="../io/xdr.html">Request JSON using Yahoo! Pipes</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/node',
name: 'events',
title: 'Handling DOM Events',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('properties');
YUI.Env.Tests.examples.push('dom-node');
YUI.Env.Tests.examples.push('store');
YUI.Env.Tests.examples.push('events');
YUI.Env.Tests.examples.push('nodelist');
YUI.Env.Tests.examples.push('ducks');
YUI.Env.Tests.examples.push('node-evt-delegation');
YUI.Env.Tests.examples.push('node-xy');
YUI.Env.Tests.examples.push('node-style');
YUI.Env.Tests.examples.push('node-insert');
YUI.Env.Tests.examples.push('node-view');
YUI.Env.Tests.examples.push('node-focusmanager-toolbar');
YUI.Env.Tests.examples.push('node-focusmanager-button');
YUI.Env.Tests.examples.push('basic-example');
YUI.Env.Tests.examples.push('photo-browser');
YUI.Env.Tests.examples.push('portal-drag');
YUI.Env.Tests.examples.push('weather');
YUI.Env.Tests.examples.push('xdr');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>