<?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>WebTigers &#187; Javascript</title>
	<atom:link href="http://webtigers.net/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://webtigers.net</link>
	<description>Professional UI</description>
	<lastBuildDate>Sun, 03 Apr 2011 10:57:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Enterprise-class JavaScript</title>
		<link>http://webtigers.net/javascript/enterprise-javascript/</link>
		<comments>http://webtigers.net/javascript/enterprise-javascript/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 18:27:24 +0000</pubDate>
		<dc:creator>K. Beau Beauchamp</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://webtigers.net/?p=139</guid>
		<description><![CDATA[What JavaScript framework or library should your team really be using? In this article, Beau Beauchamp discusses the differences between two Titans of the JavaScript world.]]></description>
			<content:encoded><![CDATA[<p>What JavaScript framework or library should your team really be using? In this article, Beau Beauchamp discusses the differences between two Titans of the JavaScript world.</p>
<h2 class="subhead">Enterprise-class JavaScript</h2>
<p>Here&#8217;s the scene: You&#8217;re the new project manager sitting at the conference table with the rest of the development team. You&#8217;ve just hired a new UI person to take the reins of your application&#8217;s front-end and deliver a product that will be a slick Web 2.0 production.</p>
<p>Mind you, the reason you hired a UI person in the first place is because your development team has been struggling with UI issues. The CSS is a mess, there are cross-browser display issues, and sometimes the DHTML works and sometimes it quits without warning. Your dev team has spent countless hours fumbling with these issues without a whole lot of success or the successes have come at a steep price in over-spent hours.</p>
<p>The discussion inevitably turns to the JavaScript and what is currently in-place within the application. Some of it is custom, some of it is jQuery, there&#8217;s an open-source piece here and purchased module for image display over there.</p>
<p>Okay, it&#8217;s a mess. You know it&#8217;s not working so you ask your UI pro for their input. The UI pro recommends a wholesale change in the JS library and a move to a full-stack framework. The rest of the dev team groans and begins to grumble. &#8220;Replace all this work!? Can&#8217;t you just fix what&#8217;s broken? That&#8217;s why we hired you!&#8221;</p>
<p>Your new UI pro simply replies, &#8220;I just fixed the problems. This is an enterprise-class application and it needs a robust framework to make the UI perform uniformly. You cannot piece these things together with scotch-tape and bubble gum. It&#8217;s all got to work together. Otherwise you end up with a mess of conflicting standards and multiple incompatible objects all trying to talk to each other through yet even more translation objects.&#8221;</p>
<p>&#8220;But we&#8217;re using a framework, jQuery!&#8221; the team lead insists.</p>
<p>Then your new UI pro drops a bombshell and says, &#8220;jQuery is not an enterprise-class framework.&#8221;</p>
<p>The room erupts into brash dissent from the developers who wrote the current UI. &#8220;Like hell it&#8217;s not! jQuery is the more widely used framework on the planet!&#8221;</p>
<p>Your UI pro allows the din to quiet. They&#8217;ve been down this road of discussion more often than they care to count, &#8220;jQuery is not the most widely used &#8216;framework&#8217; on the planet, because it&#8217;s not a &#8216;framework&#8217;, it&#8217;s a library. Big difference.&#8221;</p>
<h2 class="subhead">Clash of the Titans</h2>
<p>The above scenario is one I have had to deal with on a number of occasions.</p>
<p>As a project manager you have a lot of things to manage with your project, one of the most important is what technologies will be implemented to build the application. If you have a Java or .NET shop, those decisions are easy for the backend, you use what your engineering staff know how to use.</p>
<p>But what do you do if your engineering staff doesn&#8217;t know what to use and really doesn&#8217;t know UI all that well? Guess? Use what&#8217;s popular?</p>
<p>More often than not when I come into a shop where a project is already underway, the backend team has chosen a JavaScript library for the front-end. Generally it&#8217;s jQuery. The question I ask is, &#8220;Why did you go with jQuery?&#8221; and the answer is always the same, &#8220;Everybody uses jQuery. It&#8217;s industry standard. It&#8217;s what we know.&#8221;</p>
<p>The last part is the true reason. &#8220;It&#8217;s what we know.&#8221; The unspoken reason is also, &#8220;jQuery is super easy and helps us speed through a lot of basic DHTML we don&#8217;t have a clue about how to write otherwise.&#8221;</p>
<p>One of my primary beefs with jQuery is that it hides the nuts and bolts of true JavaScript coding and forces you to code it&#8217;s way instead of in a natural object-oriented fashion the way JavaScript was designed to work. While jQuery&#8217;s syntax is fast and easy, it&#8217;s not really JavaScript, it&#8217;s a language called jQuery that&#8217;s built on top of JavaScript.</p>
<p>Why is that a big deal? Well for starters, I&#8217;ve worked with engineers who said they knew JavaScript but the reality was if you removed the jQuery library from the application, they were hopelessly lost.</p>
<p>jQuery was really the only &#8220;JavaScript&#8221; they knew how to write. Ouch.</p>
]]></content:encoded>
			<wfw:commentRss>http://webtigers.net/javascript/enterprise-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>YUI: getElementsBy</title>
		<link>http://webtigers.net/articles/yui-getelementsby/</link>
		<comments>http://webtigers.net/articles/yui-getelementsby/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 01:10:50 +0000</pubDate>
		<dc:creator>K. Beau Beauchamp</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://webtigers.net/?p=13</guid>
		<description><![CDATA[YUI is one of the most powerful Javascript frameworks in existence today. Yet, far too many UI programmers are unaware of this true full-stack JavaScript framework and its amazing capabilities. Beau Beaucahmp discusses one of the more powerful DOM functions of the YUI library: the very cool getElementsBy DOM function. Super-Awesome Power At Your Fingertips [...]]]></description>
			<content:encoded><![CDATA[<p>YUI is one of the most powerful Javascript frameworks in existence today. Yet, far too many UI programmers are unaware of this true <a href="http://webtigers.net/javascript/enterprise-javascript/">full-stack JavaScript framework</a> and its amazing capabilities. Beau Beaucahmp discusses one of the more powerful DOM functions of the YUI library: the very cool <code>getElementsBy</code> DOM function.</p>
<h2 class="subhead">Super-Awesome Power At Your Fingertips</h2>
<p>If you&#8217;ve been programming in UI for any length of time then you&#8217;re familiar with Javascript&#8217;s built-in <code>getElementById</code> function that is used to return a single element within the DOM tree by simply passing in the element&#8217;s ID. YUI&#8217;s <code>getElementsBy</code> is quite simply <code>getElementById</code> on some serious steroids. Not only does this powerful function allow you to snap-up a single element, but it can grab a whole array of elements chosen and filtered any way you like, all in a single call.</p>
<p>But wait, there&#8217;s more! <code>getElementsBy</code> also allows you to manipulate each of the elements within your array with an optional function you supply!</p>
<h3 class="subhead">Setting-up Your Page to Use YUI</h3>
<p>First you&#8217;ll need to download and copy the core YUI files to a directory on your site. I usually like to have a <code>/javascrpt</code> or <code>/js</code> folder inside my document root that holds all of my JavaScript files. The core YUI files you&#8217;ll need will be found inside the <code>/yui/build</code> directory. I like to copy YUI&#8217;s <code>/build</code> folder to my <code>/javascript</code> folder and rename<code> /build</code> to<code> /yui</code> for convenience.</p>
<p>Now add for following code inside the <code>&lt;head&gt; ... &lt;/head&gt;</code> tags of your page:</p>
<pre class="brush:html"><script src="/yui/yahoo-dom-event/yahoo-dom-event.js"></script></pre>
<p>or you can have your page load the YUI files directly from YAHOO! using this script tag instead of the one above:</p>
<pre class="brush:js"><script src="http://yui.yahooapis.com/2.8.1/build/yahoo-dom-event/yahoo-dom-event.js"></script></pre>
<p>Now you&#8217;ll have all of the various YUI Core, DOM and Events functions at your command. Let&#8217;s look at what the <code>getElementsBy</code> function params look like:</p>
<pre class="brush:js">var Dom = YAHOO.util.Dom;
var els = Dom.getElementsBy(
    function(el){ ... },
        tagname,
        parentroot,
        function(el){ ... }
);
</pre>
<p>All of the YUI functions live in the YAHOO namespace. As such, I like to pair down my code by assigning all of the Dom and Event functions to a simple reference var, like Dom. This way, I can access the YUI DOM functions with a simple <code>Dom.getElementsBy()</code> as we see in line 2.</p>
<p>Note that <code>getElementsBy</code> can work with or without returning anything. If you do return elements, note that unlike <code>getElementById</code>, <code>getElementsBy</code> always returns an <code>array()</code> of elements. Even if you will only be returning a single element, keep in mind that it will be referenced as <code>els[0]</code>.</p>
<p>Line 3 is where the real fun begins. You begin gathering your elements with a simple boolean function that returns true or false. Let&#8217;s say I want to toggle a check or uncheck for every checkbox within a form. A good boolean function to use here would be <code>return (el.type == 'checkbox');</code>. Notice that <code>el</code> is an element that will get passed into this function to be tested. If the element has the attribute of <code>type="checkbox"</code> the function will return true and add the element to our <code>els</code> array.</p>
<p>Notice that only the first attribute of <code>getElementsBy</code> is necessary. The second and third parameters can be set to <code>null</code>. However, setting these parameters to <code>null</code> will cause <code>getElementsBy</code> to process EVERY ELEMENT on the page! This is not very efficient, especially for really large pages. That&#8217;s where the second and third parameters of this function come in handy. In line 4 of our code we can specify a specific tag name, such as <code>input</code>. In this way, the <code>getElementsBy</code> function will ignore all elements except input elements which it will then pass to the boolean function we built in the previous paragraph.</p>
<p>Even with this optimization, you still might want to further define where to begin your elements search. <code>getElmentsBy</code> allows you to optionally specify a third parameter, a root parent element to search within. If all of the elements you need to process are the children of a specific div for instance, passing in the ID string or element reference of the containing parent will limit even further where within the DOM <code>getElementsBy</code> needs to look.</p>
<p>So, with these three parameters supplied to our <code>getElmentsBy</code> function, we can collect a very specific set of elements, in our sample case we&#8217;re looking for all of the checkboxes, anywhere within the DOM of our page.</p>
<h2 class="subhead">Putting getElementsBy to Work</h2>
<p>Here&#8217;s where <code>getElmentsBy</code> becomes a real time saver. Normally, once you have built your array, you&#8217;d need to write code that loops through each of the elements within your array to do something with each element of the array; in our case we&#8217;re setting the checkbox elements to either be checked or unchecked. So why not just apply whatever function to each element as it is being added to the array built by <code>getElmentsBy</code>? Brilliant!</p>
<p>The final param of the <code>getElmentsBy</code> function is designed to do just that. Using this optional function, we can apply code to each of the elements being added to our final array. And we&#8217;re not just limited to checking or un-checking chekcboxes here. <em>Anything</em> you can do inside a function can be applied to any, all or some of the array elements. </p>
<p>Use this final function to selectively add an event listener, set element values and/or attributes (which is what we&#8217;re going to do with our checkboxes), or whatever.</p>
<pre class="brush:js">var Dom = YAHOO.util.Dom;
var els = Dom.getElementsBy(
    function(el){
        return ( el.type === 'checkbox' );
    },
    'input',
    'myform',
    function(el){
        el.checked === true;
    }
);
</pre>
<h2 class="subhead">One Call Does It All</h2>
<p>So let&#8217;s show a simple function using <code>getElmentsBy</code> that checks or un-checks all of the checkboxes on a form.</p>
<p>Here&#8217;s the example:</p>
<div class="example">
<p><script type="text/javascript">// <![CDATA[
      	var Dom = YAHOO.util.Dom; 	function toggleCheckboxes(ref){ 		Dom.getElementsBy( 			function(el){ return(el.type == 'checkbox'); }, 			'input', 			'myform', 			function(el){ el.checked = ref.checked; } 		); 	}
// ]]&gt;</script></p>
<form id="myform">
<input onclick="toggleCheckboxes(this);" name="ref" type="checkbox" /><label for="ref">Click me</label></p>
<input name="check1" type="checkbox" value="1" /><label for="check1">One</label></p>
<input name="check2" type="checkbox" value="2" /><label for="check2">Two</label></p>
<input name="check3" type="checkbox" value="3" /><label for="check3">Three</label></p>
<input name="check4" type="checkbox" value="4" /><label for="check4">Four</label></p>
<input name="check5" type="checkbox" value="5" /><label for="check5">Five</label></p>
</form>
</div>
<p>and here&#8217;s the code:</p>
<pre class="brush:html">
<script src="http://yui.yahooapis.com/2.8.1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript">
    var Dom = YAHOO.util.Dom;
    function toggleCheckboxes(ref){
        Dom.getElementsBy(
            function(el){ return(el.type == 'checkbox'); },
            'input',
            'myform',
            function(el){ el.checked = ref.checked; }
        );
    }
</script>
<form id="myform">
<input onclick="toggleCheckboxes(this);" name="ref" type="checkbox" /><label for="ref">Click me</label>
<input name="check1" type="checkbox" value="1" /><label for="ref">One</label>
<input name="check2" type="checkbox" value="2" /><label for="ref">Two</label>
<input name="check3" type="checkbox" value="3" /><label for="ref">Three</label>
<input name="check4" type="checkbox" value="4" /><label for="ref">Four</label>
<input name="check5" type="checkbox" value="5" /><label for="ref">Five</label>
    </form>
</pre>
<p>Copy and save the page as myfile.html to see how it works!</p>
<h2 class="subhead">Conclusion</h2>
<p>While other JavaScript libraries indeed have similar functions, I really like the inherent flexibility of <code>getElementsBy</code>. The beauty of YUI is that it works like JavaScript is supposed to work, it&#8217;s written they way JavaScript is written. And you don&#8217;t have to memorize a whole new syntax or funky var language to use the power of YUI.</p>
<p>____________________________</p>
<p><em>K. Beau Beauchamp is a professional UI developer with over 15 year&#8217;s experience in graphic design, marketing communications and object-oriented UI programming. Beau can be contacted via email <a href="http://webtigers.net/contact/">here</a>.</em></p>
<h2 class="subhead" style="text-align: center;"><span style="color: #999999;">&#x0416;</span></h2>
]]></content:encoded>
			<wfw:commentRss>http://webtigers.net/articles/yui-getelementsby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elegant Validation</title>
		<link>http://webtigers.net/javascript/elegant-validation/</link>
		<comments>http://webtigers.net/javascript/elegant-validation/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 18:37:29 +0000</pubDate>
		<dc:creator>K. Beau Beauchamp</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://webtigers.net/?p=146</guid>
		<description><![CDATA[Coming soon &#8230; A discussion of how to do and not do form validation.]]></description>
			<content:encoded><![CDATA[<p>Coming soon &#8230; A discussion of how to do and not do form validation.</p>
]]></content:encoded>
			<wfw:commentRss>http://webtigers.net/javascript/elegant-validation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

