<?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>Davin&#039;s blog &#187; javascript</title>
	<atom:link href="http://blog.davingranroth.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.davingranroth.com</link>
	<description>Experience design, faith, and family.</description>
	<lastBuildDate>Sat, 21 Jan 2012 13:42:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>jQuery: Show password checkbox</title>
		<link>http://blog.davingranroth.com/2010/02/jquery-show-password-checkbox/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-show-password-checkbox</link>
		<comments>http://blog.davingranroth.com/2010/02/jquery-show-password-checkbox/#comments</comments>
		<pubDate>Sat, 13 Feb 2010 16:09:41 +0000</pubDate>
		<dc:creator>Davin Granroth</dc:creator>
				<category><![CDATA[Web design and technology]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[password masking]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://blog.davingranroth.com/?p=1078</guid>
		<description><![CDATA[I wrote version 1 of a jQuery plugin during the last couple of days. Read more about jquery.showPasswordCheckbox.js. The basic functionality is to provide a checkbox on web forms to reveal the password text, so people can choose to view &#8230; <a href="http://blog.davingranroth.com/2010/02/jquery-show-password-checkbox/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I wrote version 1 of a jQuery plugin during the last couple of days. <a href="http://blog.davingranroth.com/sandbox/jquery-plugin-show-password-checkbox/">Read more about jquery.showPasswordCheckbox.js</a>.</p>
<p>The basic functionality is to provide a checkbox on web forms to reveal the password text, so people can choose to view the password they are entering as they enter it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.davingranroth.com/2010/02/jquery-show-password-checkbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Revision: Using Javascript to add instructive text for input and textarea form fields</title>
		<link>http://blog.davingranroth.com/2008/04/revision-using-javascript-to-add-instructive-text-for-input-and-textarea-form-fields/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=revision-using-javascript-to-add-instructive-text-for-input-and-textarea-form-fields</link>
		<comments>http://blog.davingranroth.com/2008/04/revision-using-javascript-to-add-instructive-text-for-input-and-textarea-form-fields/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 01:06:55 +0000</pubDate>
		<dc:creator>Davin Granroth</dc:creator>
				<category><![CDATA[Web design and technology]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.davingranroth.com/?p=361</guid>
		<description><![CDATA[
 <a href="http://blog.davingranroth.com/2008/04/revision-using-javascript-to-add-instructive-text-for-input-and-textarea-form-fields/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is a code <a href="http://blog.davingranroth.com/blog/2008/01/using-javascript-to-add-instru.html">update to a prior post</a>. </p>
<p>The Javascript I had posted earlier fell short in one important arena: when you submitted the form without entering your own text, the form would send in the instructive text. It should instead have erased that text prior to the form submission.</p>
<p>Further, in retrospect, the code was trying to do too much. It is enough to simply set a class of <code>blurred</code> and then remove it, instead of replacing it with a class of <code>sharpened</code>.</p>
<p>So, with that in mind, here is an updated set of Javascript that can be used to insert instructive or suggested texts into form fields, like text input fields or textareas.</p>
<h2>The code</h2>
<p>To start with, we need some scripts to add and remove classes values from an element. The one caveat when doing this is to remember that you can have many class names assigned in a single class attribute. So, when adding and removing class names, you have to be sure to leave pre-existing class names alone. Instead of writing these functions, I did a quick Google search for &#8220;Javascript addClass&#8221; and found these handy Javascript functions, hasClass, addClass, and removeClass, at <a href="http://www.openjs.com/scripts/dom/class_manipulation.php">openjs.com</a>. I&#8217;ll let you look there for that code.</p>
<p>With those class manipulation functions in hand, I rewrote the setSuggestedFormText function down to this.</p>
<p><code class="codeblock"><br />
/* setSuggestedFormText takes 2 arguments, mode and field.<br />
/ mode is either set to put in place the suggested text<br />
/                clear is to remove the suggested text<br />
/     the set mode also assigns adds a class of blurred<br />
/ field is the value of the ID attribute of the input or textarea element<br />
/ April 13, 2008, Davin Granroth, granroth@gmail.com.<br />
*/<br />
function setSuggestedFormText(mode, field){<br />
var key = document.getElementById(field);<br />
var defaultText = key.title;<br />
if(mode == 'set'){<br />
if(key.value == '') {<br />
addClass(key, 'blurred');<br />
key.value = defaultText;<br />
}<br />
}<br />
if(mode == 'clear'){<br />
if(key.value == defaultText) {<br />
removeClass(key, 'blurred');<br />
key.value = '';<br />
}<br />
}<br />
}</p>
<p></code></p>
<p>This function takes two arguments: <code>mode</code> and <code>field</code>. The first, <code>mode</code> can be either of <var>set</var> or <var>clear</var>. The one puts in the suggested text from the title attribute of the element, and the other clears it out. Each checks to see if the value of the field has been edited, in which case it respects the user input.</p>
<p>The next step is to set up the function which triggers this function at the appropriate events. There are three events at which the function needs to be called: when the page loads, when focus is put on the field, and when focus is removed from the field. Here is the code for setUpSuggestedFormText.</p>
<p><code class="codeblock"><br />
/*<br />
/ field is the id of input or textarea field<br />
/ parent is the id of the containing form element<br />
*/<br />
function setUpSuggestedFormText(field, parent){<br />
if(document.getElementById(field)){<br />
// This will set the text on page load<br />
setSuggestedFormText('set', field);<br />
// These 2 events will toggle between the set and clear modes<br />
document.getElementById(field).onblur = function(){<br />
setSuggestedFormText('set', field);<br />
}<br />
document.getElementById(field).onfocus = function(){<br />
setSuggestedFormText('clear', field);<br />
}<br />
// Clear the text before sumitting the form.<br />
document.getElementById(parent).onsubmit = function(){<br />
setSuggestedFormText('clear', field);<br />
}<br />
}<br />
}</p>
<p></code></p>
<p>This setUpSuggestedFormText takes two arguments: the id of the field element and the id of its parent form. The id of the parent is used in the trigger for the form submission. I had considered using a parentNode property to find it, but depending on the markup in the form, it may be multiple parentNodes up. So, this is less code. However, if someone wants to write up the function to recursively walk up the parentNode tree to find the parent form&#8217;s id, I&#8217;d be happy to incorporate it.</p>
<p>The last step is to call the setUpSuggestedFormText once the DOM has loaded in the page. One slightly intrusive means of doing this is to put a Javascript setUp function just before the closing body tag. You can, of course, choose to use something like an addDOMLoadEvent (Google for that) function. Here is what the setUp function looks like in the Javascript.</p>
<p><code class="codeblock"><br />
function setUp(){<br />
setUpSuggestedFormText('to','msgForm');<br />
setUpSuggestedFormText('from','msgForm');<br />
}</p>
<p></code></p>
<p>You can <span class="mt-enclosure mt-enclosure-file"><a href="http://davingranroth.com/blog/documents/setSuggestedFormText-3.zip">download sample code</a></span> which uses an illustration of a message form with from, to, and message fields. The download is a ZIP file which contains an XHTML file, a CSS file, and a Javascript file.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.davingranroth.com/2008/04/revision-using-javascript-to-add-instructive-text-for-input-and-textarea-form-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Javascript to add instructive text for input and textarea form fields</title>
		<link>http://blog.davingranroth.com/2008/01/using-javascript-to-add-instructive-text-for-input-and-textarea-form-fields/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-javascript-to-add-instructive-text-for-input-and-textarea-form-fields</link>
		<comments>http://blog.davingranroth.com/2008/01/using-javascript-to-add-instructive-text-for-input-and-textarea-form-fields/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 00:35:38 +0000</pubDate>
		<dc:creator>Davin Granroth</dc:creator>
				<category><![CDATA[Web design and technology]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.davingranroth.com/?p=350</guid>
		<description><![CDATA[
 <a href="http://blog.davingranroth.com/2008/01/using-javascript-to-add-instructive-text-for-input-and-textarea-form-fields/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.davingranroth.com/blog/2008/04/revision-using-javascript-to-a.html">UPDATE 2008-04-13: This code has been refactored. Please view the updated posting.</a></p>
<p>Over the last year or so, I&#8217;ve worked on a number of websites where I wanted to add instructive text to form fields, but didn&#8217;t want that text to get in the way when a person actually tries to fill out the form. So, behavior-wise, the page would load with instructive (or suggested) text in the fields. It should have a class attribute that can be used to style it so it doesn&#8217;t look like a real value (grey and italic, for instance). When someone clicks on the field, the instructive text should go away and the class should go away so the default styling kicks in. When someone clicks off, the suggested text and class should pop back in, so long as the field is empty. The instructive text should come from the title attribute on the field (that made the most sense to me, given all the options).</p>
<p>So, here are a couple Javascript functions to do so.</p>
<p><code class="codeblock"><br />
function setSuggestedFormText(mode, field) {<br />
/*<br />
mode values must be one of: set, clear, or reset<br />
set is to check the value on page load<br />
clear is to clear the default value when someone clicks on the field<br />
reset is to fill in the default value when someone clicks off the field<br />
field is the value of the ID attribute of the input or textarea element<br />
Use .blurred and .sharpened selectors in your CSS file<br />
*/<br />
var key = document.getElementById(field);<br />
var defaultText = key.title;<br />
switch(mode) {<br />
case 'set':<br />
if(key.value == '') {<br />
key.value = defaultText;<br />
key.setAttribute('class', 'blurred');<br />
}<br />
break;</p>
<p>case 'clear':<br />
if(key.value == defaultText) {<br />
key.value = '';<br />
key.setAttribute('class', 'sharpened');<br />
}<br />
break;</p>
<p>case 'reset':<br />
if(key.value == '') {<br />
key.value = defaultText;<br />
key.setAttribute('class', 'blurred');<br />
}<br />
break;</p>
<p>default:<br />
break;<br />
}</p>
<p>}<br />
function setUpSuggestedFormText(field){<br />
setSuggestedFormText('set', field);<br />
var node = document.getElementById(field);<br />
node.onblur = function(){<br />
setSuggestedFormText('reset', field);<br />
}<br />
node.onfocus = function(){<br />
setSuggestedFormText('clear', field);<br />
}<br />
}</p>
<p></code></p>
<p>To use these functions, add them to your site&#8217;s Javascript library, and then call the following functions when the document is loaded. Google for adddomloadevent or you could probably use jQuery&#8217;s document.ready function. </p>
<p>Ok, usage. Let&#8217;s say you have an input field for an email address, like so:</p>
<p><code class="codeblock"><br />
&lt;input type="text" name="email"<br />
id="email" title="Your e-mail address" /&gt;</p>
<p></code></p>
<p>You would add the following javascript code to trigger the functions.</p>
<p><code class="codeblock"><br />
// Once the DOM is loaded, call this function<br />
setUpSuggestedFormText('email');</p>
<p></code></p>
<p>Feel free to use it. If you make an improvement, please let me know.</p>
<p>To see it in use, try the search form on this page. For now, the Javascript source is at <a href="http://davingranroth.com/blog/custom.js">http://davingranroth.com/blog/custom.js</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.davingranroth.com/2008/01/using-javascript-to-add-instructive-text-for-input-and-textarea-form-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Diagram of XHTML, CSS, JavaScript as types of code in a web page</title>
		<link>http://blog.davingranroth.com/2005/10/diagram-of-xhtml-css-javascript-as-types-of-code-in-a-web-page/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=diagram-of-xhtml-css-javascript-as-types-of-code-in-a-web-page</link>
		<comments>http://blog.davingranroth.com/2005/10/diagram-of-xhtml-css-javascript-as-types-of-code-in-a-web-page/#comments</comments>
		<pubDate>Mon, 03 Oct 2005 21:36:00 +0000</pubDate>
		<dc:creator>Davin Granroth</dc:creator>
				<category><![CDATA[Web design and technology]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.davingranroth.com/?p=284</guid>
		<description><![CDATA[I&#8217;m thinking of using this diagram in an XHTML class I may be teaching in a couple weeks. The idea is to put XHTML, CSS, and Javascript in context with each other—yet to also illustrate that they are separate types &#8230; <a href="http://blog.davingranroth.com/2005/10/diagram-of-xhtml-css-javascript-as-types-of-code-in-a-web-page/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div id="attachment_479" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.davingranroth.com/wp-content/uploads/2008/12/xhtml-css-jscript-webpage.png"><img class="size-medium wp-image-479" title="XHTML, CSS, Javascript as part of a web page" src="http://blog.davingranroth.com/wp-content/uploads/2008/12/xhtml-css-jscript-webpage-300x145.png" alt="XHTML, CSS, Javascript as part of a web page" width="300" height="145" /></a><p class="wp-caption-text">XHTML, CSS, Javascript: Cumulative aspects of a web page. Click the image for a larger version.</p></div>
<p>I&#8217;m thinking of using this diagram in an XHTML class I may be teaching in a couple weeks. The idea is to put XHTML, CSS, and Javascript in context with each other—yet to also illustrate that they are separate types of code and often are actually different files altogether.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.davingranroth.com/2005/10/diagram-of-xhtml-css-javascript-as-types-of-code-in-a-web-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

