<?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>Nicolas Kuttler &#187; onclick</title>
	<atom:link href="http://www.nkuttler.de/tag/onclick/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nkuttler.de</link>
	<description>WordPress Services, IT Services</description>
	<lastBuildDate>Wed, 28 Jul 2010 23:49:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Mooified focus onload but keep backspace intact</title>
		<link>http://www.nkuttler.de/2008/10/02/mooified-focus-onload-but-keep-backspace-intact/</link>
		<comments>http://www.nkuttler.de/2008/10/02/mooified-focus-onload-but-keep-backspace-intact/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 09:46:15 +0000</pubDate>
		<dc:creator>nicolas</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[onclick]]></category>
		<category><![CDATA[onfocus]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://www.nkuttler.de//?p=142</guid>
		<description><![CDATA[You may have heard about Harmen Janssen&#8217;s technique to focus input fields on pageload and keep the backspace button&#8217;s history back function intact. I wanted to play with it any my first step was to mooify his ideas. So inside the domready event I simply did: 1 2 3 4 5 6 7 8 9 [...]]]></description>
			<content:encoded><![CDATA[<p>You may have heard about Harmen Janssen&#8217;s technique to <a href="http://www.whatstyle.net/articles/51/focus_onload_but_keep_backspace_intact">focus input fields on pageload and keep the backspace button&#8217;s history back function intact</a>. I wanted to play with it any my first step was to <a href="http://www.mootools.net">mooify</a> his ideas.<span id="more-142"></span></p>
<p>So inside the domready event I simply did:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> inputify<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    elem.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    elem.<span style="color: #660066;">addEvents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</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>event<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>event.<span style="color: #660066;">key</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'backspace'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>elem.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                history.<span style="color: #000066;">back</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
inputify<span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SomeInputfieldById'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Great, reusable, everthing works fine. Ok, so let&#8217;s incorporate my ideas from my <a href="http://www.nkuttler.de//2008/09/20/html-forms-and-onclickonfocus/">HTML forms and onclick/onfocus</a> post (basically, select on click instead of focus and pre-fill the input field):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> inputify<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> startvalue <span style="color: #339933;">=</span> elem.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
    elem.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    elem.<span style="color: #660066;">addEvents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</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>event<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>event.<span style="color: #660066;">key</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'backspace'</span> <span style="color: #339933;">&amp;&amp;</span> elem.<span style="color: #660066;">value</span> <span style="color: #339933;">===</span> startvalue<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                history.<span style="color: #000066;">back</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">'click'</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            elem.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
inputify<span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SomeInputfieldById'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Great works fine. Remember pre-filled value and only go back in history when the user hasn&#8217;t changed it. Done.</p>
<p>Wait&#8230; oh, noes! The backspace button is supposed to <span style="text-decoration: underline">delete</span> the input. Now, most users will unwillingly trigger the history back function. That&#8217;s as broken as it gets&#8230; Well, in theory I could focus the input field instead of selecting it, and use something like caretPos, and only go back in history if it&#8217;s at the start&#8230; But that&#8217;s not what I had in mind (well not sure <u>what</u> I was thinking anyway&#8230;)</p>
<p>So I consider this technique unusable for the time being for my own sites. The only times I focus some input field onload is for search functions anyway. There, I&#8217;d rather annoy a few users and improve the overall usability of the site. Like Harmen said in his post, don&#8217;t use onload input focus just because you can <img src='http://www.nkuttler.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.nkuttler.de/2008/10/02/mooified-focus-onload-but-keep-backspace-intact/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML forms and onclick/onfocus</title>
		<link>http://www.nkuttler.de/2008/09/20/html-forms-and-onclickonfocus/</link>
		<comments>http://www.nkuttler.de/2008/09/20/html-forms-and-onclickonfocus/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 08:34:39 +0000</pubDate>
		<dc:creator>nicolas</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[onclick]]></category>
		<category><![CDATA[onfocus]]></category>
		<category><![CDATA[usability]]></category>
		<category><![CDATA[Webdesign]]></category>

		<guid isPermaLink="false">http://www.nkuttler.de//?p=17</guid>
		<description><![CDATA[When you use HTML forms it is often desirable to pre-fill some input fields. Your idea could be that you want to give your visitors some clue to what they&#8217;re supposed to fill in.This looks nice, for example: When the user clicks into the field you&#8217;ll want to remove the clue. The obvious way to [...]]]></description>
			<content:encoded><![CDATA[<p>When you use HTML forms it is often desirable to pre-fill some input fields. Your idea could be that you want to give your visitors some clue to what they&#8217;re supposed to fill in.<span id="more-17"></span>This looks nice, for example:</p>
<form>
<input type="text" value="Your email" /></form>
<p>When the user clicks into the field you&#8217;ll want to remove the clue. The obvious way to do this is to add an onclick JavaScript command:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.value=''&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Your email&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<form>
<input onclick="this.value=''" type="text" value="Your email" /></form>
<p>The user can still access the input field through other means than clicking. He can use the tab key for example. That&#8217;s why usually the onfocus event is more useful:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.value=''&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Your email&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<form>
<input onfocus="this.value=''" type="text" value="Your email" />
</form>
<p>This seems to work nicely. However there&#8217;s still a problem. When a user fills in his email address, reads a little more on your site and clicks the input field again, his address will be removed again! So to make sure this doesn&#8217;t happen we add a little check for the content of the field:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Your email&quot;</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (this.value == 'Your email') {this.value=''}&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>And here it is:</p>
<form>
<input type="text" value="Your email" onfocus="if (this.value == 'Your email') {this.value=''}" />
</form>
<a name="wptoc_0_0_0"></a><h2>Restore default value if nothing is entered</h2>
<p>Ok, now this is working. However, when your visitor decides not to input any data, the field stays empty. If you have no label for the input he or she might forget what the field was for. We fix this by restoring the default value when the input field is left:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Your email&quot;</span> <span style="color: #000066;">onblur</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if(this.value == '') { this.value='Your email'}&quot;</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (this.value == 'Your email') {this.value=''}&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<form>
<input type="text" value="Your email" onblur="if(this.value == '') { this.value='Your email'}" onfocus="if (this.value == 'Your email') {this.value=''}" />
</form>
<p>Thanks to Ray for his example linked in the comments.</p>
<a name="wptoc_0_0_1"></a><h2>Select onclick</h2>
<p>But sometimes this isn&#8217;t the best solution. It could be more desirable to highlight the content of the input field if you&#8217;re offering some kind of search. The user may not want to start a new search but to modify the existing one.</p>
<p>By highlighting the input field the user can choose to either empy it by starting to type or to modify it by using the arrow keys.</p>
<form>
<input onfocus="this.select()" type="text" value="Your previous searcj" />
</form>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.select()&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Your previous searcj&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<a name="wptoc_0_0_2"></a><h2>Dynamic input color</h2>
<p>This is a little advanced. Let&#8217;s assume you want the default value to be in a different color and change it onclick. This is one working solution:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color: #f00&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Your email&quot;</span> <span style="color: #000066;">onblur</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if(this.value == '') { this.style.color='#f00'; this.value='Your email'}&quot;</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (this.value == 'Your email') {this.style.color='#0f0'; this.value=''}&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<form>
<input style="color: #f00" type="text" value="Your email" onblur="if(this.value == '') { this.style.color='#f00'; this.value='Your email'}" onfocus="if (this.value == 'Your email') {this.style.color='#0f0'; this.value=''}" />
</form>
<p>Keep in mind that this might create problems when you reload the page. Some browsers will remember the value of the input field, and the color will be different even though the field does not contain the default value. Fixing that is beyond the scope of this post. See <tt>onload</tt> and domready events.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nkuttler.de/2008/09/20/html-forms-and-onclickonfocus/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
