<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The focus and blur Event Fix</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>
<a href="#toc" class="jump">Jump to Table of Contents</a>
<h1>The focus and blur Event Fix</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><div class="intro">
<p>The <code>event-focus</code> module supplies a behavior patch for the native
<code>focus</code> and <code>blur</code> events to allow them to bubble for event delegation.</p>
</div>
<h2 id="bubble-time">Bubble time</h2>
<p>Most DOM events bubble. If, for example, you click on a button in a form,
the click event will be dispatched to event subscribers on the button, on the
fieldset, on the form, on the div containing the form, and so on up to the
<code>document</code>. You can subscribe to the click event at any level and your
callback will be executed. But <code>focus</code> and <code>blur</code> aren't like that. They are
only dispatched to subscribers on the element that received or lost focus.</p>
<p>But <a href="delegation.html">we like event delegation</a> so we determined
a way to synthesize bubbling for <code>focus</code> and <code>blur</code> using <a
href="http://www.w3.org/TR/DOM-Level-3-Events/#event-flow">capture phase</a>
subscriptions in non-IE browsers, and proprietary focus-related events in IE
that do bubble. The result should be transparent for individual element
<code>focus</code> and <code>blur</code> subscriptions, but make <code>focus</code> and <code>blur</code> work as you would
expect when subscribing higher in the document tree.</p>
<pre class="code prettyprint"><ul id="items">
<li><input value="form elements are focusable by default"></li>
<li><a href="yahoo.com">Links are focusable by default</a></li>
<li tabindex="2">Things with `tabindex` 0 or higher are made user focusable</li>
<li tabindex="-1">Things with `tabindex` -1 can be focused programatically</li>
<li>But by default none of those `focus` events will report to #items</li>
</ul>
<script>
YUI().use('node-base', function (Y) {
var items = Y.one('#items');
items.on('focus', function (e) {
Y.log("The list does not have a tabindex, so this will never execute");
});
Y.use('event-focus', function () {
items.on('focus', function (e) {
Y.log("But now that event-focus is in place, future " +
"subscriptions like this one will.");
// e.target received focus
});
});
});
</script></pre>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div id="toc" class="sidebox">
<div class="hd">
<h2 class="no-toc">Table of Contents</h2>
</div>
<div class="bd">
<ul class="toc">
<li>
<a href="#bubble-time">Bubble time</a>
</li>
</ul>
</div>
</div>
<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: 'event',
title: 'The focus and blur Event Fix',
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>