<?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>KirkHings.com &#187; html</title>
	<atom:link href="http://kirkhings.com/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://kirkhings.com</link>
	<description>The '-berger' part trips up so many folks that I sometimes simplify my name for you. Yes, just for you.</description>
	<lastBuildDate>Sat, 13 Jun 2009 18:35:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Slideshow from Images Array</title>
		<link>http://kirkhings.com/code/javascript/slideshow-from-images-array/380/</link>
		<comments>http://kirkhings.com/code/javascript/slideshow-from-images-array/380/#comments</comments>
		<pubDate>Fri, 15 May 2009 19:57:23 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=380</guid>
		<description><![CDATA[ photo credit: pawpaw67
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/7641646@N03/1302964167/" title="Thistle Seeds" target="_blank"><img src="http://farm2.static.flickr.com/1007/1302964167_a809f4945f.jpg" alt="Thistle Seeds" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-sa/2.0/" title="Attribution-ShareAlike License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/7641646@N03/1302964167/" title="pawpaw67" target="_blank">pawpaw67</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a JavaScript image slideshow with next and previous links that use an array. Loop the script so the slideshow never reaches an end.
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;NASA Patches Slideshow&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div align=&quot;center&quot;&gt;
		&lt;h1&gt;NASA Patches Slideshow&lt;/h1&gt;
		&lt;img src=&quot;images/nasa.jpeg&quot; id=&quot;currentPicture&quot; width=&quot;201&quot; height=&quot;155&quot; alt=&quot;Slideshow&quot; /&gt;
		&lt;h2&gt;
        	&lt;a href=&quot;previous.html&quot; id=&quot;prevLink&quot;&gt;&amp;laquo; Previous&lt;/a&gt;&amp;nbsp;&amp;nbsp;
            &lt;a href=&quot;next.html&quot; id=&quot;nextLink&quot;&gt;Next &amp;raquo;&lt;/a&gt;&lt;/h2&gt;
	&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
window.onload = initLinks;

// create array of pictures/strings
var myPics = new Array(&quot;images/nasa.jpeg&quot;, &quot;images/columbia1.gif&quot;, &quot;images/columbia2.jpeg&quot;, &quot;images/columbia3.jpg&quot;, &quot;images/columbia4.jpeg&quot;, &quot;images/challenger.jpeg&quot;);

// track currently displayed image, set to 0
var thisPic = 0;

// handle behaviors when next and previous links are clicked
function initLinks() {
	// when prevLink is clicked, call function to fetch the previous pic
	document.getElementById(&quot;prevLink&quot;).onclick = processPrevious;
	// when nextLink is clicked, call function to fetch the next pic
	document.getElementById(&quot;nextLink&quot;).onclick = processNext;
}

// called when prevLink is clicked
function processPrevious() {
	// if we are on slide 0
	if (thisPic == 0) {
		// then set thisPic to be same value as array length (meaning last picture)
		// this allows looping around the collection not reaching an end
		thisPic = myPics.length;
	}
	// decrement thisPic to be one less than last pic so as to point to previous
	thisPic--;
	// set source of page's displayed picture to be new current
	document.getElementById(&quot;currentPicture&quot;).src = myPics[thisPic];
	// js handled the work, so prevent next page from being fetched
	return false;
}

// called when nextLink is clicked
function processNext() {
	// incerement thisPic to be one more than last pic so as to point to next
	thisPic++;
	// if we are on last picture in array
	if (thisPic == myPics.length) {
		// then set thisPic to be original value in array (meaning first picture)
		// this allows looping around the collection not reaching an end
		thisPic = 0;
	}
	// set source of page's displayed picture to be new current
	document.getElementById(&quot;currentPicture&quot;).src = myPics[thisPic];
	// js handled the work, so prevent next page from being fetched
	return false;
}
</pre>
<p>Here is a screenshot example of a previous slideshow image:<br />
<div id="attachment_382" class="wp-caption alignleft" style="width: 466px"><img src="http://kirkhings.com/wp-content/uploads/js-nasapatches2.png" alt="Example of the previous image fetched" title="js-nasapatches2" width="456" height="306" class="size-full wp-image-382" /><p class="wp-caption-text">Example of the previous image fetched</p></div></p>
<p>Here is a screenshot example of a next slideshow image:<br />
<div id="attachment_381" class="wp-caption alignleft" style="width: 466px"><img src="http://kirkhings.com/wp-content/uploads/js-nasapatches1.png" alt="Example of the next image fetched" title="js-nasapatches1" width="456" height="306" class="size-full wp-image-381" /><p class="wp-caption-text">Example of the next image fetched</p></div></p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/slideshow-from-images-array/380/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disjointed JavaScript Rollover Causes Separate Animation</title>
		<link>http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/</link>
		<comments>http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 19:57:17 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=324</guid>
		<description><![CDATA[ photo credit: Matt McGee
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/51035750608@N01/183123043/" title="July 5, 2006: Catawampus" target="_blank"><img src="http://farm1.static.flickr.com/70/183123043_35ef17f96c.jpg" alt="July 5, 2006: Catawampus" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-nd/2.0/" title="Attribution-NoDerivs License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/51035750608@N01/183123043/" title="Matt McGee" target="_blank">Matt McGee</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a JavaScript image rollover animation which includes a default image, and a hover over image. Use abstracted functions. Improve accessibility by creating onfocus and onblur event handlers for users without mice. Make the rollover occur when you mouse over a different document element.
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;A Disjointed Link Rollover&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;h1&gt;
		&lt;a href=&quot;next.html&quot; id=&quot;arrow&quot;&gt;Next page&lt;/a&gt;
	&lt;/h1&gt;
	&lt;img src=&quot;images/arrow_off.gif&quot; width=&quot;147&quot; height=&quot;82&quot; id=&quot;arrow_image&quot; alt=&quot;arrow&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
}
img {
	border-width: 0;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
// initialize the rollovers when window loads
window.onload = rolloverInit;

function rolloverInit() {
	// loop through all links on page
	for (var i=0; i&lt;document.links.length; i++) {
		// capture i-th link as local variable to make life easier later on
		var linkObject = document.links[i];
		// if current linkObject has an id (meaning it is something we care to change)
		if (linkObject.id) {
			// append current image's id with _image (preplanned format), save to local variable
			var imageObject = document.getElementById(linkObject.id + &quot;_image&quot;);
			// if the imageObject exists
			if (imageObject) {
				// pass both current local variables to setupRollover function
				setupRollover(linkObject, imageObject);
			}
		}
	}
}

// 'thisLink &amp; thisImage' passed from rolloverInit, meaning we need to setup the rollover for this particular link &amp; image
function setupRollover(thisLink, thisImage) {
	// capture the current link's associated image as thisLink's intended target image property
	thisLink.imgToChange = thisImage;
	// capture rollOut function as behavior when thisLinks's onmouseout event handler is triggered
	thisLink.onmouseout = rollOut;
	// capture rollOver function as behavior when thisImage's onmouseover event handler is triggered
	thisLink.onmouseover = rollOver;

	// create new child image object for thisLink's  outImage property
	thisLink.outImage = new Image();
	// store current image src as the source for overImage, so it stays same when not rolled over
	thisLink.outImage.src = thisImage.src;

	// create new child image object for thisLink's  overImage property
	thisLink.overImage = new Image();
	// store current image src as the source for overImage, so it stays same when not rolled over
	thisLink.overImage.src = &quot;images/&quot; + thisLink.id + &quot;_on.gif&quot;;

	// improve accessibility
	// simply set thisLink's onblur and onfocus event handlers to rollOut and rollOver respectively
	thisLink.onblur = rollOut;
	thisLink.onfocus = rollOver;
}	

function rollOut() {
	// change thisLinks's image src to the outImage source property (already captured)
	this.imgToChange.src = this.outImage.src;
}

function rollOver() {
	// change thisLinks's image src to the overImage source property (already captured)
	this.imgToChange.src = this.overImage.src;
}
</pre>
<p>And finally here is a screenshot of the link highlighted result:<br />
<img src="http://kirkhings.com/wp-content/uploads/linkrollover.png" alt="linkrollover" title="linkrollover" width="398" height="181" class="alignleft size-full wp-image-325" /></p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessible JavaScript Rollover with Three-State Functionality</title>
		<link>http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/</link>
		<comments>http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 18:55:08 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[accessibility]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=319</guid>
		<description><![CDATA[ photo credit: Rhian vK
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/34764035@N00/2258204466/" title="PRIORITE" target="_blank"><img src="http://farm3.static.flickr.com/2391/2258204466_3162c3f37f.jpg" alt="PRIORITE" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/34764035@N00/2258204466/" title="Rhian vK" target="_blank">Rhian vK</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a JavaScript image rollover animation which includes a default image, a hover over image, and an image when the link is clicked. Use abstracted functions. Improve accessibility by creating onfocus and onblur event handlers for users without mice.
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;An Accessible Three-State Rollover&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;a href=&quot;next1.html&quot;&gt;
		&lt;img src=&quot;images/button1_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button1&quot; id=&quot;button1&quot; /&gt;
	&lt;/a&gt;&amp;nbsp;&amp;nbsp;
	&lt;a href=&quot;next2.html&quot;&gt;
		&lt;img src=&quot;images/button2_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button2&quot; id=&quot;button2&quot; /&gt;
		&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
}
img {
	border-width: 0;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
// initialize the rollovers when window loads
window.onload = rolloverInit;

function rolloverInit() {
	// loop through all images on page
	for (var i = 0; i &lt; document.images.length; i++) {
		// if current image is wrapped in an anchor tag (i.e. &lt;a href=&quot;&quot;&gt;&lt;img&gt;&lt;/a&gt;)
		if (document.images[i].parentNode.tagName == &quot;A&quot;) {
			// initiate rollover setup for this current image, pass the current image to that function
			setupRollover(document.images[i]);
		}
	}
}

// 'thisImage' passed from rolloverInit, meaning we need to setup the rollover for this particular image
function setupRollover(thisImage) {
	// create new child image object for thisImage object's outImage property
	thisImage.outImage = new Image();
	// store current image src as the source for outimage, so it stays same when not rolled over
	thisImage.outImage.src = thisImage.src;
	// capture rollOut function as behavior when thisImage's onmouseout event handler is triggered
	thisImage.onmouseout = rollOut;

	// create new child image object for thisImage object's overImage property
	thisImage.overImage = new Image();
	// store preconsructed '_on'-state image src (using thisImage's captured id/name) as the source for overimage, so it stays changes when rolled over
	thisImage.overImage.src = &quot;images/&quot; + thisImage.id + &quot;_on.gif&quot;;
	// capture rollOver function as behavior when thisImage's onmouseover event handler is triggered
	thisImage.onmouseover = rollOver;

	// create new child image object for thisImage object's clickImage property
	thisImage.clickImage = new Image();
	// store preconsructed '_on'-state image src (using thisImage's captured id/name) as the source for overimage, so it stays changes when rolled over
	thisImage.clickImage.src = &quot;images/&quot; + thisImage.id + &quot;_click.gif&quot;;
	// capture rollClick function as behavior when thisImage's onmousedown event handler is triggered
	thisImage.onmousedown = rollClick;

	// improve accessibility
	// set image's parent anchor child Image to thisImage
	thisImage.parentNode.childImage = thisImage;
	// when image's achor tag is unselected (an event handler)
	thisImage.parentNode.onblur = rollOutChild;
	// when image's achor tag is selected (an event handler)
	thisImage.parentNode.onfocus = rollOverChild;
}

function rollOut() {
	// change thisImage's src to the outImage source property (already captured)
	this.src = this.outImage.src;
}

function rollOver() {
	// change thisImage's src to the overImage source property (already captured)
	this.src = this.overImage.src;
}

function rollClick() {
	// change thisImage's src to the clickImage source property (already captured)
	this.src = this.clickImage.src;
}

function rollOutChild() {
	// change the achor tag's childImage src to the childImage's outImage source property (already captured)
	this.childImage.src = this.childImage.outImage.src;
}

function rollOverChild() {
	// change the achor tag's childImage src to the childImage's overImage source property (already captured)
	this.childImage.src = this.childImage.overImage.src;
}
</pre>
<p>And finally here is a screenshot of the improved end rollover result: </p>
<p>Rollover state A: <div id="attachment_301" class="wp-caption alignleft" style="width: 278px"><img src="http://kirkhings.com/wp-content/uploads/improvedrollovera.png" alt="Improved rollover state A" title="improvedrollovera" width="268" height="73" class="size-full wp-image-301" /><p class="wp-caption-text">Improved rollover state A</p></div></p>
<p>
Rollover state B: <div id="attachment_302" class="wp-caption alignleft" style="width: 263px"><img src="http://kirkhings.com/wp-content/uploads/improvedrolloverb.png" alt="Improved rollover state B" title="improvedrolloverb" width="253" height="51" class="size-full wp-image-302" /><p class="wp-caption-text">Improved rollover state B</p></div></p>
<p>
Rollover state C (clicked button): <div id="attachment_313" class="wp-caption alignleft" style="width: 264px"><img src="http://kirkhings.com/wp-content/uploads/improvedrolloverc.png" alt="Button state when button is clicked" title="improvedrolloverc" width="254" height="51" class="size-full wp-image-313" /><p class="wp-caption-text">Button state when button is clicked</p></div></p>
<div id="attachment_320" class="wp-caption alignleft" style="width: 408px"><img src="http://kirkhings.com/wp-content/uploads/accessiblerollover.png" alt="Demonstrating accessible rollover when button is selected but not moused over." title="accessiblerollover" width="398" height="58" class="size-full wp-image-320" /><p class="wp-caption-text">Demonstrating accessible rollover when button is selected but not moused over.</p></div>
<p></p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Three-State JavaScript Rollover</title>
		<link>http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/</link>
		<comments>http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 16:47:36 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=312</guid>
		<description><![CDATA[ photo credit: chimothy27
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/8641421@N07/2787799499/" title="Will Rollover" target="_blank"><img src="http://farm4.static.flickr.com/3036/2787799499_6bc46bda11.jpg" alt="Will Rollover" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-nd/2.0/" title="Attribution-NoDerivs License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/8641421@N07/2787799499/" title="chimothy27" target="_blank">chimothy27</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a JavaScript image rollover animation which includes a default image, a hover over image, and an image when the link is clicked. Use abstracted functions.
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;A Three-State Rollover&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;a href=&quot;next1.html&quot;&gt;
		&lt;img src=&quot;images/button1_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button1&quot; id=&quot;button1&quot; /&gt;
	&lt;/a&gt;&amp;nbsp;&amp;nbsp;
	&lt;a href=&quot;next2.html&quot;&gt;
		&lt;img src=&quot;images/button2_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button2&quot; id=&quot;button2&quot; /&gt;
		&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
}
img {
	border-width: 0;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
// initialize the rollovers when window loads
window.onload = rolloverInit;

function rolloverInit() {
	// loop through all images on page
	for (var i = 0; i &lt; document.images.length; i++) {
		// if current image is wrapped in an anchor tag (i.e. &lt;a href=&quot;&quot;&gt;&lt;img&gt;&lt;/a&gt;)
		if (document.images[i].parentNode.tagName == &quot;A&quot;) {
			// initiate rollover setup for this current image, pass the current image to that function
			setupRollover(document.images[i]);
		}
	}
}

// 'thisImage' passed from rolloverInit, meaning we need to setup the rollover for this particular image
function setupRollover(thisImage) {
	// create new child image object for thisImage object's outImage property
	thisImage.outImage = new Image();
	// store current image src as the source for outimage, so it stays same when not rolled over
	thisImage.outImage.src = thisImage.src;
	// capture rollOut function as behavior when thisImage's onmouseout event handler is triggered
	thisImage.onmouseout = rollOut;

	// create new child image object for thisImage object's overImage property
	thisImage.overImage = new Image();
	// store preconsructed '_on'-state image src (using thisImage's captured id/name) as the source for overimage, so it stays changes when rolled over
	thisImage.overImage.src = &quot;images/&quot; + thisImage.id + &quot;_on.gif&quot;;
	// capture rollOver function as behavior when thisImage's onmouseover event handler is triggered
	thisImage.onmouseover = rollOver;

	// create new child image object for thisImage object's clickImage property
	thisImage.clickImage = new Image();
	// store preconsructed '_on'-state image src (using thisImage's captured id/name) as the source for overimage, so it stays changes when rolled over
	thisImage.clickImage.src = &quot;images/&quot; + thisImage.id + &quot;_click.gif&quot;;
	// capture rollClick function as behavior when thisImage's onmousedown event handler is triggered
	thisImage.onmousedown = rollClick;
}

function rollOut() {
	// change thisImage's src to the outImage source property (already captured)
	this.src = this.outImage.src;
}

function rollOver() {
	// change thisImage's src to the overImage source property (already captured)
	this.src = this.overImage.src;
}

function rollClick() {
	// change thisImage's src to the clickImage source property (already captured)
	this.src = this.clickImage.src;
}
</pre>
<p>And finally here is a screenshot of the improved end rollover result: </p>
<p>Rollover state A: <div id="attachment_301" class="wp-caption alignleft" style="width: 278px"><img src="http://kirkhings.com/wp-content/uploads/improvedrollovera.png" alt="Improved rollover state A" title="improvedrollovera" width="268" height="73" class="size-full wp-image-301" /><p class="wp-caption-text">Improved rollover state A</p></div></p>
<p>Rollover state B: <div id="attachment_302" class="wp-caption alignleft" style="width: 263px"><img src="http://kirkhings.com/wp-content/uploads/improvedrolloverb.png" alt="Improved rollover state B" title="improvedrolloverb" width="253" height="51" class="size-full wp-image-302" /><p class="wp-caption-text">Improved rollover state B</p></div></p>
<p>Rollover state C (clicked button): <div id="attachment_313" class="wp-caption alignleft" style="width: 264px"><img src="http://kirkhings.com/wp-content/uploads/improvedrolloverc.png" alt="Button state when button is clicked" title="improvedrolloverc" width="254" height="51" class="size-full wp-image-313" /><p class="wp-caption-text">Button state when button is clicked</p></div></p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple and Improved Image Rollovers</title>
		<link>http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/</link>
		<comments>http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 17:24:14 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=298</guid>
		<description><![CDATA[ photo credit: Etwood
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/44788018@N00/2874889018/" title="Ziggy" target="_blank"><img src="http://farm4.static.flickr.com/3278/2874889018_5846e2b179.jpg" alt="Ziggy" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/44788018@N00/2874889018/" title="Etwood" target="_blank">Etwood</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a simple javascript image rollover animation. (Two arrow images colored red and blue provided in my set, but you can use any two images you like.) Do basic version first, then improve it by separating script from display code, and abstract the functions.
</p></blockquote>
<p><strong>The Simple Method: </strong></p>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;A Simple Rollover&lt;/title&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;a href=&quot;next.html&quot; onmouseover=&quot;document.arrow.src='images/arrow_on.gif'&quot; onmouseout=&quot;document.arrow.src='images/arrow_off.gif'&quot;&gt;&lt;img src=&quot;images/arrow_off.gif&quot; width=&quot;147&quot; height=&quot;82&quot; alt=&quot;arrow&quot; name=&quot;arrow&quot; /&gt;&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
}
img {
	border-width: 0;
}
</pre>
<p>This is a quick and dirty method, but it has problems. The biggest problem is the rollover does not execute until the user moves their mouse over the image. If the new image is large or the user&#8217;s connection is slow, there will be a noticeable delay in the first time rollover.</p>
<p>Another problem is that are now committed to updating JavaScript code within HTML code. This causes many efficiency hassles, such as intermingling design work with scripting work, and violating Model-View-Controller (MVC) principles.</p>
<p>A better method is to separate the JavaScript from the HTML page. This next improved method will attempt Don&#8217;t Repeat Yourself (DRY) principles.</p>
<p><strong>The Improved Method: </strong></p>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;An Improved Rollover&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;a href=&quot;next1.html&quot;&gt;
		&lt;img src=&quot;images/button1_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button1&quot; id=&quot;button1&quot; /&gt;
	&lt;/a&gt;&amp;nbsp;&amp;nbsp;
	&lt;a href=&quot;next2.html&quot;&gt;
		&lt;img src=&quot;images/button2_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button2&quot; id=&quot;button2&quot; /&gt;
		&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
}
img {
	border-width: 0;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
// initialize the rollovers when window loads
window.onload = rolloverInit;

function rolloverInit() {
	// loop through all images on page
	for (var i = 0; i &lt; document.images.length; i++) {
		// if current image is wrapped in an anchor tag (i.e. &lt;a href=&quot;&quot;&gt;&lt;img&gt;&lt;/a&gt;)
		if (document.images[i].parentNode.tagName == &quot;A&quot;) {
			// initiate rollover setup for this current image, pass the current image to that function
			setupRollover(document.images[i]);
		}
	}
}

// 'thisImage' passed from rolloverInit, meaning we need to setup the rollover for this particular image
function setupRollover(thisImage) {
	// create new child image object for thisImage object's outImage property
	thisImage.outImage = new Image();
	// store current image src as the source for outimage, so it stays same when not rolled over
	thisImage.outImage.src = thisImage.src;
	// capture rollOut function as behavior when thisImage's onmouseout event handler is triggered
	thisImage.onmouseout = rollOut;

	// create new child image object for thisImage object's overImage property
	thisImage.overImage = new Image();
	// store preconsructed '_on'-state image src (using thisImage's captured id/name) as the source for overimage, so it stays changes when rolled over
	thisImage.overImage.src = &quot;images/&quot; + thisImage.id + &quot;_on.gif&quot;;
	// capture rollOver function as behavior when thisImage's onmouseover event handler is triggered
	thisImage.onmouseover = rollOver;
}

function rollOut() {
	// change thisImage's src to the outImage source property (already captured)
	this.src = this.outImage.src;
}

function rollOver() {
	// change thisImage's src to the overImage source property (already captured)
	this.src = this.overImage.src;
}
</pre>
<p>And finally here is a screenshot of the improved end rollover result: </p>
<p>Rollover state A: <div id="attachment_301" class="wp-caption alignleft" style="width: 278px"><img src="http://kirkhings.com/wp-content/uploads/improvedrollovera.png" alt="Improved rollover state A" title="improvedrollovera" width="268" height="73" class="size-full wp-image-301" /><p class="wp-caption-text">Improved rollover state A</p></div></p>
<p>Rollover state B: <div id="attachment_302" class="wp-caption alignleft" style="width: 263px"><img src="http://kirkhings.com/wp-content/uploads/improvedrolloverb.png" alt="Improved rollover state B" title="improvedrolloverb" width="253" height="51" class="size-full wp-image-302" /><p class="wp-caption-text">Improved rollover state B</p></div></p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random BINGO Card Generator with Ajax Reset</title>
		<link>http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/</link>
		<comments>http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 17:17:10 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[randomizer]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=224</guid>
		<description><![CDATA[ photo credit: Keees
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/54366416@N00/6443146/" title="bingo" target="_blank"><img src="http://farm1.static.flickr.com/6/6443146_de0b591435.jpg" alt="bingo" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/54366416@N00/6443146/" title="Keees" target="_blank">Keees</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a BINGO card using HTML and JavaScript. Generate random integers between 1 and 75, and insert them into each of 24 squares, skipping the center FREE space. Integer ranges are as follow: B = 1-15, I = 16-30, N = 31-45, G = 46-60, O = 67-75. Ensure no two squares contain duplicate numbers. Bonus: After initial card load, enable resetting the card without a server call (quasi-Ajax-powered).
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Make Your Own Bingo Card&lt;/title&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;script.css&quot; /&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;
	&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Create A Bingo Card&lt;/h1&gt;
&lt;table&gt;
	&lt;tr&gt;
		&lt;th width=&quot;20%&quot;&gt;B&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;I&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;N&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;G&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;O&lt;/th&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square0&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square1&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square2&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square3&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square4&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square5&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square6&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square7&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square8&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square9&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square10&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square11&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;free&quot;&gt;Free&lt;/td&gt;
		&lt;td id=&quot;square12&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square13&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square14&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square15&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square16&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square17&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square18&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square19&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square20&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square21&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square22&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square23&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;a href=&quot;script.html&quot; id=&quot;reload&quot;&gt;Click here&lt;/a&gt; to create a new card&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
	color: black;
	font-size: 20px;
	font-family: &quot;Lucida Grande&quot;, Verdana, Arial, Helvetica, sans-serif;
}

h1, th {
	font-family: Georgia, &quot;Times New Roman&quot;, Times, serif;
}

h1 {
	font-size: 28px;
}

table {
	border-collapse: collapse;
}

th, td {
	padding: 10px;
	border: 2px #666 solid;
	text-align: center;
	font-size: 24px;
}

#free {
	background-color: #ffff00;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
window.onload = initializePage;

// even though arrays are 0-based, creating 76 positions generates elements 0-75, we will later ignore position 0
// note that newly created arrays have all elements set to false
var usedNumbers = new Array(76);

function initializePage() {
	// so older browsers do not get 24 error messages
	if (document.getElementById) {
		// enable creating a new card without reloading page
		document.getElementById(&quot;reload&quot;).onclick = anotherCard;
		// generate the new bingo card
		newCard();
	} else {
		alert(&quot;Sorry, but your browser is not running JavaScript, and this game requires it.&quot;);
	}
}

function newCard() {
	for (var i = 0; i &lt; 24; i++) {
		// pass the current i as parameter to setSquare function
		setSquare(i);
	}
}

function setSquare(thisSquare) {
	// capture current corresponding html square (via id=)  to current digit that was passed over
	var currentSquare = &quot;square&quot; + thisSquare;

	// while bingo numbers are ranged 1-75, letter columns are restricted to specific number ranges
	// b = 1-15, i = 16-30, n = 31-45, g = 46-60, o = 67-75
	// OR mathematically we can generate a random number between 1-15 and add column-specific number
	// b = 0, i = 15, n = 30, g = 45, o = 60	

	// need 5 sets of numbers 0-4 (1 set for each letter)
	// note missing '2,' from third set, so as to skip the middle 'free' square
	var columnPlace = new Array(0,1,2,3,4,0,1,2,3,4,0,1,3,4,0,1,2,3,4,0,1,2,3,4);

	// capture appropriate column mulitple by referencing current square # as array element
	var columnMultiple = (columnPlace[thisSquare] * 15);

	// initialize variable to be used in following do-while loop
	var newNumber;

	// generate newNumber and check if it has been used already, if so loop again
	do {
		// add random number to the particular column multiple
		newNumber = columnMultiple + getNewNumber()  + 1;
	} while(usedNumbers[newNumber]);

	// prevent duplicate numbers by testing if the newNumber has already been used once on this card
	if (!usedNumbers[newNumber]) {
		// if duplicate was not found, this must be an original #, so set this element to true
		usedNumbers[newNumber] = true;

		// assign randomized newNum to current square
		document.getElementById(currentSquare).innerHTML = newNumber;
	}
}

function getNewNumber() {
	// capture random number between 1 and 15 for bingo values
		// math.random() returns random number between 0 and 1
		// math.floor() returns floor of a decimal number (ex. 1.75 becomes 1)
	return Math.floor(Math.random() * 15)
}

function anotherCard() {
	// housekeeping: reset the usedNumbers flag arrays to all false
	for (var i = 1; i &lt; usedNumbers.length; i++) {
		usedNumbers[i] = false;
	}

	// generate the new bingo card
	newCard();
	return false;
}
</pre>
<p>And finally here is a screenshot of the end result: </p>
<div id="attachment_231" class="wp-caption alignleft" style="width: 400px"><img src="http://kirkhings.com/wp-content/uploads/bingocard.png" alt="BINGO card screenshot" title="bingocard" width="390" height="417" class="size-full wp-image-231" /><p class="wp-caption-text">BINGO card screenshot</p></div>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
