<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nicomputer&#039;s blog &#187; jQuery</title>
	<atom:link href="http://blog.nicomputer.com/articles/web-integration/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nicomputer.com</link>
	<description>I am (not?) a Robot!</description>
	<lastBuildDate>Tue, 08 Dec 2009 13:53:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>How to give the focus with jQuery?</title>
		<link>http://blog.nicomputer.com/web-integration/accessibility/how-to-give-the-focus-with-jquery/</link>
		<comments>http://blog.nicomputer.com/web-integration/accessibility/how-to-give-the-focus-with-jquery/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 14:31:18 +0000</pubDate>
		<dc:creator>--Nico</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[accessibility jQuery]]></category>

		<guid isPermaLink="false">http://blog.nicomputer.com/?p=14</guid>
		<description><![CDATA[For the past two years, I have been interesting and working on accessibility issues in all my web development projects. Not because my employer or my clients understand the real importance of offering accessible websites (do they?) but because I believe in an accessible world (and an accessible Internet) for <strong>EVERYBODY</strong>.

There are lots of articles and resources on the Internet which talk about the theory of the accessibility matter. However, there are not many technical articles which explain how the standards are really implemented or how a web developer has to implement them.

This first article focuses on how to give the... focus on a non-focusable <acronym title="Hypertext Markup Language">HTML</acronym> element with <a href="http://www.jquery.com/">jQuery</a>.

It extends (and I hope, completes) the <a href="http://donaldevans.com/guide/bestpractices/movefocus2.html">original article</a> written by C. Blouch and published on the <a href="http://donaldevans.com/guide/bestpractices/">Donald F. Evans's website</a>.]]></description>
			<content:encoded><![CDATA[<p>For the past two years, I have been interesting and working on accessibility issues in all my web development projects. Not because my employer or my clients understand the real importance of offering accessible websites <span class="wcag">(do they?)</span> but because I believe in an accessible world (and an accessible Internet) for <strong>EVERYBODY</strong>.</p>
<p>There are lots of articles and resources on the Internet which talk about the theory of the accessibility matter. However, there are not many technical articles which explain how the standards are really implemented or how a web developer has to implement them.</p>
<p>This first article focuses on how to give the&#8230; focus on a non-focusable <acronym title="Hypertext Markup Language">HTML</acronym> element with <a href="http://www.jquery.com/">jQuery</a>.</p>
<p>It extends (and I hope, completes) the <a href="http://donaldevans.com/guide/bestpractices/movefocus2.html">original article</a> written by C. Blouch and published on the <a href="http://donaldevans.com/guide/bestpractices/">Donald F. Evans&#8217;s website</a>.</p>
<p><span id="more-14"></span></p>
<h2>The theory</h2>
<p>Giving the focus to a non-focusable <acronym title="Hypertext Markup Language">HTML</acronym> element seems to be quite simple. Many articles on the Web were written about it.</p>
<p>To summarize them, you only have to :</p>
<ol>
<li>Set the <code>tabindex</code> attribute of the selected element to <code>-1</code></li>
<li>Give the focus to the <acronym title="Document Object Model">DOM</acronym> node with the JavaScript method <code>.focus()</code></li>
</ol>
<p>Quite simple, isn&#8217;t it?</p>
<p>In fact, after doing few tests with Jaws, it demonstrates that it does not really work that way&#8230;</p>
<h2>The reality</h2>
<p>By default, only a few <acronym title="Hypertext Markup Language">HTML</acronym> elements are focusable like anchors or form inputs.<br />
So, in order to give the focus to a non-focusable <acronym title="Hypertext Markup Language">HTML</acronym> element correctly, don&#8217;t forget to add an important third step:</p>
<ol>
<li>Set the <code>tabindex</code> attribute of the selected element to <code>-1</code></li>
<li>Give the focus to the <acronym title="Document Object Model">DOM</acronym> node with the JavaScript method <code>focus()</code></li>
<li><strong>Use the <code>setTimeout</code> JavaScript function to delay the execution of the <code>focus()</code> function</strong></li>
</ol>
<p>Without the use of the <code>setTimeout</code> function, Jaws will simply ignore the focus and will not redirect the user to the element where you want to give the focus.</p>
<h2>The JavaScript code</h2>
<pre class="brush: jscript; title: ;">
$(document).ready(function () {

	$(&amp;quot;#addNMoveFocus&amp;quot;).click(function(e){

		AddNMoveFocus(&amp;quot;ul#nl&amp;quot;, &amp;quot;&amp;lt;li&amp;gt;&amp;lt;b&amp;gt;A simple b tag inside a li tag&amp;lt;/b&amp;gt;&amp;lt;/li&amp;gt;&amp;quot;, &amp;quot;ul li:last b&amp;quot;);
		e.preventDefault();

	});

});

function AddNMoveFocus (o1, content, o2){
	AddContent(o1, content);
	MoveFocus(o2);
}

function AddContent(o, content) {
	$(o).append(content);
}

var f; // must be declared as global
function MoveFocus(o){
	$(o).attr(&amp;quot;tabindex&amp;quot;, &amp;quot;-1&amp;quot;);
	f = $(o).get(0);
	setTimeout(&amp;quot;f.focus();&amp;quot;, 0);
}
</pre>
<h2>The examples</h2>
<p>Examples listed below were tested with IE7, Jaws 7.10, Jaws 8, Jaws 9 and Jaws 10</p>
<ul>
<li><a href="http://demo.nicomputer.com/accessibility/focus/">View the working example (with the use of <code>setTimeout</code> function).</a></li>
<li><a href="http://demo.nicomputer.com/accessibility/focus/index-1.html">View the not working example (without the use of <code>setTimeout</code> function).</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.nicomputer.com/web-integration/accessibility/how-to-give-the-focus-with-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

