<?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>Sylvan Korvus &#187; jquery</title>
	<atom:link href="http://www.korvus.com/blog/tags/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.korvus.com</link>
	<description></description>
	<lastBuildDate>Fri, 25 Nov 2011 01:43:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Making the Tab Key work with JEditable Fields</title>
		<link>http://www.korvus.com/blog/geek/making-the-tab-key-work-with-jeditable-fields/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=making-the-tab-key-work-with-jeditable-fields</link>
		<comments>http://www.korvus.com/blog/geek/making-the-tab-key-work-with-jeditable-fields/#comments</comments>
		<pubDate>Sun, 24 May 2009 16:35:42 +0000</pubDate>
		<dc:creator>Sylvan</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.korvus.com/blog/news/making-the-tab-key-work-with-jeditable-fields/</guid>
		<description><![CDATA[Recently I was asked to solve a problem someone was having with JEditable. There were several fields on the page that existed as double-clickable text, which could then edited. The problem was, the tab key would not move between the fields. The first step of course was to build a simple test environment: a page [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was asked to solve a problem someone was having with <a href="http://www.appelsiini.net/projects/jeditable">JEditable</a>. There were several fields on the page that existed as double-clickable text, which could then edited. The problem was, the tab key would not move between the fields.</p>
<p>The first step of course was to build a simple test environment: a page with several divs with ids such as “box1” “box2”  etc., <a href="http://jquery.com/">JQuery</a> and JEditable called, and JEditable configured to talk to a simple PHP script that simply echoes its input:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><span id="more-157"></span><br />
That was all straightforward enough, and replicated the basics of the client’s environment. The real trouble was the tab key functionality. Picking up a tab keypress is easy enough; with basic Javascript, or in JQuery:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.editbox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keydown'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">keyCode</span><span style="color: #339933;">==</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//code</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There were two primary problems:</p>
<ol>
<li>Moving the focus out of the existing field being edited.</li>
<li>Activating the next editable field.</li>
</ol>
<p>#1 took a fair amount of trial and error for me. JEditable creates a new input field inside the container element, and none of the JQuery selectors I was trying to use: children, sibling, etc. were working for me. Finally, I managed to get it this way:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I’ll be the first to admit that’s a little bit brute-force. If anyone has a more elegant way of selecting the created input in a JEditable element, please let me know!</p>
<p>Problem #2 evaded solution. I looked inside the JEditable code, and it is activated by an event; there was no function I could call directly, that I could see anyway, that would activate the next element. It took putting the problem aside for a few hours before it came to me.</p>
<p>JQuery nicely lets you create mouse events and other events. So the solution then became fairly obvious:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>nextBox<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dblclick</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Where <em>nextBox</em> is the next box in line. <em>Note: JEditable normally takes a single click, so the “click()” function would be sufficient here. In this case, it’s configured for double-clicks.</em></p>
<p>It worked perfectly. Tab in one field would blur the current field, then activate the next field. Here’s the basic code, all together:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.editbox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keydown'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">keyCode</span><span style="color: #339933;">==</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> nextBox<span style="color: #339933;">=</span><span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
             <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.editbox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.editbox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    nextBox<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.editbox:first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         <span style="color: #006600; font-style: italic;">//last box, go to first</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                    nextBox<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.editbox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">//Next box in line</span>
                <span style="color: #009900;">&#125;</span>
            $<span style="color: #009900;">&#40;</span>nextBox<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dblclick</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//Go to assigned next box</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>           <span style="color: #006600; font-style: italic;">//Suppress normal tab</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I’ve omitted some very straightforward code to detect if shift is held down, and go up and wrap around to the bottom. But this should be sufficient to get anyone started with adding tab key functionality to their JEditable based forms.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.korvus.com/blog/geek/making-the-tab-key-work-with-jeditable-fields/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

