<?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; Formular</title>
	<atom:link href="http://www.nicolaskuttler.de/tag/formular/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nicolaskuttler.de</link>
	<description>IT Dienstleistungen, Saarbrücken</description>
	<lastBuildDate>Sat, 12 Jun 2010 21:42:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>HTML Formulare und onclick/onfocus Ereignisse</title>
		<link>http://www.nicolaskuttler.de/2009/08/26/html-formulare-und-onclickonfocus-ereignisse/</link>
		<comments>http://www.nicolaskuttler.de/2009/08/26/html-formulare-und-onclickonfocus-ereignisse/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 10:07:52 +0000</pubDate>
		<dc:creator>nicolas</dc:creator>
				<category><![CDATA[Technik]]></category>
		<category><![CDATA[Formular]]></category>
		<category><![CDATA[Formulare]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[onclick]]></category>
		<category><![CDATA[onfocus]]></category>

		<guid isPermaLink="false">http://www.nicolaskuttler.de/?p=3</guid>
		<description><![CDATA[Wenn man HTML Formulare auf eine Seite stellt kann es hilfreich sein, Eingabefelder in Voraus auszufüllen. Man kann seinen Besuchern so einen Hinweis darauf geben, welche Daten sie eingeben sollen. Dies sieht zum Beispiel hilfreich aus: &#60;form&#62; &#60;input onclick=&#34;this.value=''&#34; type=&#34;text&#34; value=&#34;Ihre E-Mail&#34; /&#62; &#60;/form&#62; Wenn der Benutzer das Feld anklickt sollte der Hinweis entfernt werden. [...]]]></description>
			<content:encoded><![CDATA[<p>Wenn man HTML Formulare auf eine Seite stellt kann es hilfreich sein, Eingabefelder in Voraus auszufüllen. Man kann seinen Besuchern so einen Hinweis darauf geben, welche Daten sie eingeben sollen.<span id="more-411"></span> Dies sieht zum Beispiel hilfreich aus:</p>
<form>
<input type="text" value="Ihre E-Mail" /></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;">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;Ihre E-Mail&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>Wenn der Benutzer das Feld anklickt sollte der Hinweis entfernt werden. Dies kann mit mit dem <strong>onclick</strong> JavaScript Ereignis geschehen:</p>
<form>
<input onclick="this.value=''" type="text" value="Ihre E-Mail" /></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.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;Ihre E-Mail&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>Unsere Besucher können die Eingabefelder aber auch anders erreichen. Die <tt>Tab</tt> Taste kann beispielsweise benutzt werden, um zwischen den Feldern zu navigieren. Daher ist das <strong>onfocus</strong> Ereignis besser für unsere Zwecke geeignet:</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;Ihre E-Mail&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="Ihre E-Mail" />
</form>
<p>Die scheint gut zu funktionieren. Es gibt aber immer noch ein Problem wenn der Besucher seine E-Mail Adresse eingibt, und dann etwas anderes auf unserer Seite liest. Klickt er das Feld dann noch einmal an verschwindet seine Vorherige Eingabe. Um dies zu verhindern überprüfen wir, ob sich der Inhalt des Textfelds geändert hat:</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;Ihre E-Mail&quot;</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (this.value == 'Ihre E-Mail')  {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>Hier das Beispiel:</p>
<form>
<input type="text" value="Ihre E-Mail" onfocus="if (this.value == 'Ihre E-Mail')  {this.value=''}" />
</form>
<a name="wptoc_0_0_0"></a><h2>Den Ausgangstext wiederherstellen wenn nichts eingegeben wurde</h2>
<p>Wunderbar, so weit funktioniert alles. Was passiert aber, wenn unser Besucher das Feld anklickt und dann nichts eingibt? Das Feld bleibt einfach leer. Wenn das Formular keine weiteren Beschreibungen hat kann der Benutzer vergessen haben, welche Informationen in das Feld sollen. Wir umgehen diesen Fall indem wir den ursprünglichen Wert wiederherstellen, wenn das Feld verlassen wird und nichts eingegeben wurde:</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;Ihre E-Mail&quot;</span> <span style="color: #000066;">onblur</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if(this.value == '') { this.       value='Ihre E-Mail'}&quot;</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (this.value == 'Ihre E-Mail') {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="Ihre E-Mail" onblur="if(this.value == '') { this.       value='Ihre E-Mail'}" onfocus="if (this.value == 'Ihre E-Mail') {this.value=''}" />
</form>
<a name="wptoc_0_0_1"></a><h2>Text beim anklicken auswählen</h2>
<p>Bei einigen Eingabefelder kann etwas anderes nützlich sein, beispielsweise den Text auszuwählen. Wenn wir dies tun kann der Besucher den Feldinhalt entweder löschen oder bearbeiten:</p>
<form>
<input onfocus="this.select()" type="text" value="Vorherige Suche" />
</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;Vorherige Suche&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>Dynamische Farben</h2>
<p>Dies ist ein etwas komplizierteres Thema. Nehmen wir an wir wollen den Hinweistext in einer anderen Farbe haben als die Eingabe des Benutzers. Hier ist ein Weg um dies zu erreichen:</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;Ihre E-Mail&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='Ihre E-Mail'}&quot;</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (this.value  == 'Ihre E-Mail') {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="Ihre E-Mail" onblur="if(this.value  == '') { this.style.color='#f00'; this.value='Ihre E-Mail'}" onfocus="if (this.value  == 'Ihre E-Mail') {this.style.color='#0f0'; this.value=''}" />
</form>
<p>Hier muss man im Auge behalten, dass beim erneuten Laden der Seite Probleme auftreten können. Manche Browser werden vorher vom Benutzer eingegeben Werte automatisch wiederherstellen. Dann haben wir diese Werte jedoch in der falschen Farbe.<br />
Möchte man dies verhindern kann man zu <strong>onload</strong> oder ähnlichen JavaScript Ereignissen greifen.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nicolaskuttler.de/2009/08/26/html-formulare-und-onclickonfocus-ereignisse/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
