<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example: Simple 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: Simple DOM Events</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><style scoped>
#container {
background-color: #F5E3B1;
border: 1px solid #C2AE6D;
height: 76px;
/*padding: 1em 0 0 1em;*/
padding-left: 1em;
cursor: pointer;
}
#demo .hello {
background: url("../assets/event/earth.png") no-repeat scroll 141px -62px #000000;
color: #C3A7CD;
border-color: #000;
font-size: 150%;
}
#example-canvas #demo a {
color: #00c;
text-decoration: underline;
}
.message {
visibility: hidden;
}
</style>
<div class="intro">
<p>Clicking in the yellow box will give hello world feedback.
Clicking on the first link will take you to the YUI website; clicking on the
second link, which has the same <code>href</code> attribute, will display
a message instead, and not navigate to a new page.</p>
<p>Event Utility is used here to do two things:</p>
<ol>
<li>Attach the <code>click</code> event handler to the yellow box;</li>
<li>Attach an event handler to the second <code><a></code>
element that uses <code>preventDefault()</code> to prevent the link,
when clicked, from navigating to a new page. </li>
</ol>
</div>
<div class="example yui3-skin-sam">
<div id="demo">
<div id="container">
<p>Click for Hello World test.</p>
</div>
<p><a href="http://yuilibrary.com" id="firstA">The YUI Library. (Link navigates away from page.)</a></p>
<a href="http://yuilibrary.com" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a>
<div class="message">
When you clicked on the second link, *preventDefault* was called, so it did not navigate away from the page.
</div>
</div>
<script>
YUI().use('node', function (Y) {
// A function that gives hello world feedback:
var helloWorld = function(e) {
e.target.setHTML("<p>Hello World!</p>");
Y.one('#container').addClass('hello');
}
// subscribe the helloWorld function as an event handler for the click
// event on the container div:
var node = Y.one("#container");
node.on("click", helloWorld);
// A function that displays a message and prevents the default behavior of
// the event for which it is a handler:
var interceptLink = function(e) {
e.preventDefault();
Y.one('.message').setStyle('visibility', 'visible');
}
// subscribe our interceptLink function as a click handler for the second
// anchor element:
Y.one('#secondA').on("click", interceptLink);
});
</script>
</div>
<h2>Reacting to the <code>click</code> event</h2>
<p>To illustrate event handling syntax, we'll create a <code><div></code> and
give some feedback when that <code><div></code> is clicked.</p>
<pre class="code prettyprint">// Create a YUI instance and load the 'node' module
YUI().use('node', function (Y) {
// A function that gives hello world feedback:
var helloWorld = function(e) {
e.target.setHTML("<p>Hello World!</p>");
Y.one('#container').addClass('hello');
}
...</pre>
<p>We now use the Node's <code>on</code> method to attach our
<code>helloWorld</code> function as a handler for the <code>click</code> event.
<pre class="code prettyprint">// Point to the container div with the CSS selector
var node = Y.one('#container');
node.on("click", helloWorld);</pre>
<p>Almost all event handling begins with a premise just this simple: we have an
element to which something might happen (eg, it might be clicked) and, when
that <em>does</em> happen, we want to do something (eg,
<code>helloWorld</code>).</p>
<h2 id="prevent">Preventing event behavior</h2>
<p>In some cases, you may want to replace the default behavior of an event.
For example, let's say you have two links on the page:</p>
<pre class="code prettyprint"><p><a href="http://yuilibrary.com/" id="firstA">The YUI Library. (Link navigates away from page.)</a></p>
<p><a href="http://yuilibrary.com/" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a></p></pre>
<p>Let's say that when the second link is clicked, instead of navigating away
from the current page, you want to display a message.
The event object passed to your event handler is a facade — not
the actual browser event object.
This facade supports the <code>preventDefault</code> method for cancelling
inherent browser behavior such as anchor links loading a new page.</p>
<pre class="code prettyprint">// A function that stops the browser from navigating away
// from the page, and instead displays a message.
var interceptLink = function(e) {
e.preventDefault();
Y.one('.message').setStyle('visibility', 'visible');
}
// subscribe our interceptLink function to the second anchor
Y.one('#secondA').on("click", interceptLink);</pre>
<h2 id="fullcode">Full Code Listing</h2>
<pre class="code prettyprint"><div id="demo">
<div id="container">
<p>Click for Hello World test.</p>
</div>
<p><a href="http://yuilibrary.com" id="firstA">The YUI Library. (Link navigates away from page.)</a></p>
<a href="http://yuilibrary.com" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a>
<div class="message">
When you clicked on the second link, *preventDefault* was called, so it did not navigate away from the page.
</div>
</div>
<script>
YUI().use('node', function (Y) {
// A function that gives hello world feedback:
var helloWorld = function(e) {
e.target.setHTML("<p>Hello World!</p>");
Y.one('#container').addClass('hello');
}
// subscribe the helloWorld function as an event handler for the click
// event on the container div:
var node = Y.one("#container");
node.on("click", helloWorld);
// A function that displays a message and prevents the default behavior of
// the event for which it is a handler:
var interceptLink = function(e) {
e.preventDefault();
Y.one('.message').setStyle('visibility', 'visible');
}
// subscribe our interceptLink function as a click handler for the second
// anchor element:
Y.one('#secondA').on("click", interceptLink);
});
</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="Use the Event Utility to attach simple DOM event handlers.">
<a href="basic-example.html">Simple DOM Events</a>
</li>
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed.">
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a>
</li>
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events">
<a href="swipe-example.html">Supporting A Swipe Left Gesture</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 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="Shows how to extend the base widget class, to create your own Widgets.">
<a href="../widget/widget-extend.html">Extending the Base Widget Class</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 data over HTTP.">
<a href="../io/get.html">HTTP GET to request data</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/event',
name: 'basic-example',
title: 'Simple DOM Events',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('basic-example');
YUI.Env.Tests.examples.push('synth-example');
YUI.Env.Tests.examples.push('swipe-example');
YUI.Env.Tests.examples.push('node-focusmanager-button');
YUI.Env.Tests.examples.push('widget-extend');
YUI.Env.Tests.examples.push('photo-browser');
YUI.Env.Tests.examples.push('portal-drag');
YUI.Env.Tests.examples.push('get');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>