<?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>Mark Needham &#187; jQuery</title>
	<atom:link href="http://www.markhneedham.com/blog/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Mon, 13 Feb 2012 21:25:48 +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: Collecting the results from a collection of asynchronous requests</title>
		<link>http://www.markhneedham.com/blog/2011/09/25/jquery-collecting-the-results-from-a-collection-of-asynchronous-requests/</link>
		<comments>http://www.markhneedham.com/blog/2011/09/25/jquery-collecting-the-results-from-a-collection-of-asynchronous-requests/#comments</comments>
		<pubDate>Sun, 25 Sep 2011 09:26:19 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3732</guid>
		<description><![CDATA[Liz and I recently spent some time building a pair stair to show how long ago people had paired with each other and one of the things we had to do was make AJAX requests to get the pairing data for each person and then collate it all to build the stair. The original attempt [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://lizdouglass.wordpress.com/">Liz</a> and I recently spent some time building a pair stair to show how long ago people had paired with each other and one of the things we had to do was make AJAX requests to get the pairing data for each person and then collate it all to build the stair.</p>
<div align="center">
<img src="http://www.markhneedham.com/blog/wp-content/uploads/2011/09/pair-stair.gif" alt="Pair stair" title="pair-stair.gif" border="0" width="545" height="341" />
</div>
<p>The original attempt to do this looked a bit like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;Marc&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Liz&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Ken&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Duncan&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Uday&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Mark&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Charles&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> grid <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>people<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">,</span> person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/git/pairs/'</span> <span style="color: #339933;">+</span> person<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// parse data and create somethingCool</span>
    grid.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>somethingCool<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// do something with grid</span></pre></div></div>

<p>When we try to do something with <cite>grid</cite> it is of course empty because we&#8217;ve attempted to access it before all of the callbacks (which populate it) have returned.</p>
<p>Pedro Teixeira has <a href="http://metaduck.com/post/2675027550/asynchronous-iteration-patterns-in-node-js">a nice blog post which explains how to solve this problem in node.js</a> and we can use the same pattern here.</p>
<p>We need to write our own looping mechanism which is able to determine when the last callback has returned.</p>
<p>This is done by creating a copy of the <cite>people</cite> array and then manually iterating through it using <cite>shift</cite>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;Marc&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Liz&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Ken&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Duncan&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Uday&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Mark&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Charles&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> peopleCopy <span style="color: #339933;">=</span> people.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> grid <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> getPairs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> peopleCopy.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>peopleCopy.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// do something with grid</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/git/pairs&quot;</span> <span style="color: #339933;">+</span> person<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// parse data and create somethingCool</span>
      grid.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>somethingCool<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      getPairs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>						
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I tried to extract the asynchronous looping and ended up with the following function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> asyncLoop<span style="color: #009900;">&#40;</span>collection<span style="color: #339933;">,</span> seedResult<span style="color: #339933;">,</span> loopFn<span style="color: #339933;">,</span> completionFn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> copy <span style="color: #339933;">=</span> collection.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">=</span> copy.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>copy.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      completionFn<span style="color: #009900;">&#40;</span>seedResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      loopFn<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> seedResult<span style="color: #339933;">,</span> loop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>	
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which could be called like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;Marc&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Liz&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Ken&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Duncan&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Uday&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Mark&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Charles&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
asyncLoop<span style="color: #009900;">&#40;</span>people<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> grid<span style="color: #339933;">,</span> callBackFn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// parse data and create something cool</span>
  grid.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>somethingCool<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  callBackFn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>grid<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// do something with grid</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;m not sure that it reads that much clearer but it does push some of the boiler plate code away.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/09/25/jquery-collecting-the-results-from-a-collection-of-asynchronous-requests/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>jQuery UI Tabs: Changing selected tab</title>
		<link>http://www.markhneedham.com/blog/2010/09/08/jquery-ui-tabs-changing-selected-tab/</link>
		<comments>http://www.markhneedham.com/blog/2010/09/08/jquery-ui-tabs-changing-selected-tab/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 18:32:37 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2901</guid>
		<description><![CDATA[We&#8217;re using the tabs part of the jQuery UI library on the project I&#8217;m currently working on and one thing we wanted to do was change the default tab that was being selected. The documentation suggested that one way to do this was to give the index of the tab we wanted selected when calling [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re using the <a href="http://jqueryui.com/demos/tabs/">tabs part of the jQuery UI library</a> on the project I&#8217;m currently working on and one thing we wanted to do was change the default tab that was being selected. </p>
<p>The documentation suggested that one way to do this was to give the index of the tab we wanted selected when calling the tabs function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;.selector&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">tabs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> selected<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Since we wanted to select the tab by name based on a value from the query string we thought it would probably be simpler if we could just set the selected tab using a css class.</p>
<p>Our initial thought was that we could put the &#8216;ui-tabs-hide&#8217; class on the divs that we wanted to hide and then not put that class on the one that we wanted to show.</p>
<p>Unfortunately that didn&#8217;t work and the first tab was still being selected&#8230;</p>
<p>We downloaded version <a href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.js">1.8.2</a> of the library  via <a href="http://code.google.com/apis/libraries/devguide.html#jqueryUI">Google&#8217;s CDN</a> (which seems really cool!) and were able to see that our class was actually intentionally being removed!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>10523
10524
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">selected</span> <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">anchors</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// check for length avoids error when initializing empty list</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">panels</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">selected</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ui-tabs-hide'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Luckily a little further down the file there is a comment which explains some other ways to manipulate the selected tab:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>10749
10750
10751
10752
10753
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">			<span style="color: #006600; font-style: italic;">// Selected tab</span>
			<span style="color: #006600; font-style: italic;">// use &quot;selected&quot; option or try to retrieve:</span>
			<span style="color: #006600; font-style: italic;">// 1. from fragment identifier in url</span>
			<span style="color: #006600; font-style: italic;">// 2. from cookie</span>
			<span style="color: #006600; font-style: italic;">// 3. from selected class attribute on &lt;li&gt;</span></pre></td></tr></table></div>

<p>We need to put the class &#8216;ui-tabs-selected&#8217; on the appropriate &lt;li&gt; and then that will be the one that gets selected.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/09/08/jquery-ui-tabs-changing-selected-tab/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>jQuery: Dynamically updating a drop down list</title>
		<link>http://www.markhneedham.com/blog/2010/06/30/jquery-dynamically-updating-a-drop-down-list/</link>
		<comments>http://www.markhneedham.com/blog/2010/06/30/jquery-dynamically-updating-a-drop-down-list/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 10:46:20 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2611</guid>
		<description><![CDATA[We recently had a requirement to dynamically update a drop down list based on how the user had filled in other parts of the page. Our initial approach was to populate the drop down with all potential options on page load and then add CSS selectors to the options that we wanted to hide. That [...]]]></description>
			<content:encoded><![CDATA[<p>We recently had a requirement to dynamically update a drop down list based on how the user had filled in other parts of the page.</p>
<p>Our initial approach was to populate the drop down with all potential options on page load and then add CSS selectors to the options that we wanted to hide. That worked fine in Chrome and Firefox but Internet Explorer seems to ignore CSS selectors inside a drop down list so none of the options were being hidden.</p>
<p>We therefore had to try and dynamically add and remove options from the drop down list instead.</p>
<p>The list that we initially loaded onto the page was like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;select id=&quot;foo&quot; name=&quot;foo&quot;&gt;
&lt;option value=&quot;A&quot; selected=&quot;selected&quot;&gt;A&lt;/option&gt;
&lt;option value=&quot;B&quot;&gt;B&lt;/option&gt;
&lt;option value=&quot;C&quot;&gt;C&lt;/option&gt;
&lt;option value=&quot;Not applicable&quot;&gt;Not applicable&lt;/option&gt;
&lt;/select&gt;</pre></div></div>

<p><a href="http://twitter.com/christianralph">Christian</a> eventually came up with the following solution to hide/show those options and select the appropriate one after several hours of trial and error:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// captured on page load</span>
<span style="color: #003366; font-weight: bold;">var</span> originalFooOptions <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo &gt; option&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> dependentField <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#fieldFooIsDependentOn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>notApplicable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	foo.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;option[value!='Not applicable']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	foo.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Not applicable&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	foo.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span>originalFooOptions<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> newOption <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> dependentField.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			newOption.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;selected&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;selected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		newOption.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Another approach we tried was to try and dynamically update &#8216;foo&#8217; rather than removing all the items and then adding them back.</p>
<p>Unfortunately we kept getting an &#8216;unspecified error&#8217; on Internet Explorer 6 when we tried to set the selected value with that approach.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/06/30/jquery-dynamically-updating-a-drop-down-list/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>jQuery: $.post, &#8216;jsonp&#8217; and cross-domain requests</title>
		<link>http://www.markhneedham.com/blog/2009/08/27/jquery-post-jsonp-and-cross-domain-requests/</link>
		<comments>http://www.markhneedham.com/blog/2009/08/27/jquery-post-jsonp-and-cross-domain-requests/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 12:39:26 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1585</guid>
		<description><![CDATA[We spent a bit of time yesterday looking through the jQuery code trying to work out why a cross domain request we were making using jQuery&#8217;s &#8216;$.post&#8217; function wasn&#8217;t working. In hindsight perhaps it should have been obvious that you wouldn&#8217;t be able to do that but I didn&#8217;t completely understand how we were able [...]]]></description>
			<content:encoded><![CDATA[<p>We spent a bit of time yesterday looking through the jQuery code trying to work out why a cross domain request we were making using jQuery&#8217;s &#8216;$.post&#8217; function wasn&#8217;t working.</p>
<p>In hindsight perhaps it should have been obvious that you wouldn&#8217;t be able to do that but I didn&#8217;t completely understand how we were able to do cross domain requests were possible at all but we had some &#8216;$.getJson&#8217; &#8216;jsonp&#8217; function calls around our code base which were doing just that.</p>
<p>The <a href="http://docs.jquery.com/Ajax/jQuery.post">jQuery documentation</a> seemed to suggest it was possible to do a &#8216;jsonp&#8217; &#8216;POST&#8217; request but when we tried to do so we got the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Error: uncaught exception: [Exception... &quot;Access to restricted URI denied&quot; code: &quot;1012&quot; nsresult: &quot;0x805303f4 (NS_ERROR_DOM_BAD_URI)&quot;</pre></div></div>

<p>The failure was occurring in this part of the code on line 3517 inside the &#8216;ajax&#8217; function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3512
3513
3514
3515
3516
3517
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">		<span style="color: #006600; font-style: italic;">// Open the socket</span>
		<span style="color: #006600; font-style: italic;">// Passing null username, generates a login popup on Opera (#2865)</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> s.<span style="color: #660066;">username</span> <span style="color: #009900;">&#41;</span>
			xhr.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span> s.<span style="color: #660066;">url</span><span style="color: #339933;">,</span> s.<span style="color: #660066;">async</span><span style="color: #339933;">,</span> s.<span style="color: #660066;">username</span><span style="color: #339933;">,</span> s.<span style="color: #660066;">password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">else</span>
			xhr.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span> s.<span style="color: #660066;">url</span><span style="color: #339933;">,</span> s.<span style="color: #660066;">async</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I had initially thought that passing in &#8216;jsonp&#8217; to the function did something clever to fool the browser into sending the Xml Http Request but actually in a <a href="http://groups.google.com/group/jquery-dev/browse_thread/thread/e7eb4a23eef342fb?pli=1">thread from the jQuery mailing list</a> from a year ago where Michael Geary explains what&#8217;s actually happening:</p>
<blockquote><p>
Cross-domain JSONP isn&#8217;t AJAX at all. It doesn&#8217;t use XMLHttpRequest. It&#8217;s nothing more than a dynamic script element that loads JavaScript code.</p>
<p>You can&#8217;t do a POST with a dynamic script element. Where would you put the POST data?</p>
<p>I don&#8217;t know what the $.ajax code is trying to do &#8211; maybe it should fail in a more informative way. It will fail one way or another regardless.
</p></blockquote>
<p>We could see where this was being done in the jQuery code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">		<span style="color: #006600; font-style: italic;">// If we're requesting a remote document</span>
		<span style="color: #006600; font-style: italic;">// and trying to load JSON or Script with a GET</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> s.<span style="color: #660066;">dataType</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;script&quot;</span> <span style="color: #339933;">&amp;&amp;</span> type <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;GET&quot;</span> <span style="color: #339933;">&amp;&amp;</span> parts
			<span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> location.<span style="color: #660066;">protocol</span> <span style="color: #339933;">||</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> location.<span style="color: #660066;">host</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">var</span> head <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;head&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> script <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;script&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			script.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> s.<span style="color: #660066;">url</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s.<span style="color: #660066;">scriptCharset</span><span style="color: #009900;">&#41;</span>
				script.<span style="color: #660066;">charset</span> <span style="color: #339933;">=</span> s.<span style="color: #660066;">scriptCharset</span><span style="color: #339933;">;</span>
&nbsp;
			...
&nbsp;
			<span style="color: #660066;">head</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>script<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			...
		<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>On line 3477 a script tag is dynamically added into the page and on line 3478 we set &#8216;src&#8217; to be the url for our cross domain request.</p>
<p>We can see on line 3473 that this only happens if we have a &#8216;GET&#8217; request.</p>
<p>It turned out for us that we were actually only doing this cross domain request on one of our early test environments and that in latter test environments we have everything running on the same domain. </p>
<p>On this occasion we have decided to stop using this environment since it&#8217;s not reflective of what our application will run like in production but if we wanted to do a cross domain request then we would need to make use of the &#8216;$.get&#8217; method with &#8216;jsonp&#8217; passed as an option.</p>
<p>I was talking about this with Dave Yeung at our coding dojo last night and he pointed me to <a href="https://developer.mozilla.org/En/HTTP_Access_Control">an article describing how Firefox 3.5 is now supporting the &#8216;access control for cross site requests&#8217; recommendation</a> which will <a href="http://dev.w3.org/2006/waf/access-control/">allow cross domain XHR requests to happen</a> by providing some extra header tags and then reading some additional tags sent back in the response where the server on the other domain can decide which domains are allowed to make calls to it.</p>
<p>I&#8217;m still learning this stuff so if anything I&#8217;ve said isn&#8217;t accurate please point that out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/08/27/jquery-post-jsonp-and-cross-domain-requests/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>jQuery: Approaches to testing</title>
		<link>http://www.markhneedham.com/blog/2009/01/24/jquery-approaches-to-testing/</link>
		<comments>http://www.markhneedham.com/blog/2009/01/24/jquery-approaches-to-testing/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 23:36:32 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=876</guid>
		<description><![CDATA[We&#8217;ve been doing a bit of work with jQuery and true to our TDD roots we&#8217;ve been trying to work out the best way to test drive our coding in this area. There seem to be 3 main ways that you can go about doing this, regardless of the testing framework you choose to you. [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been doing a bit of work with <a href="http://jquery.com/">jQuery</a> and true to our TDD roots we&#8217;ve been trying to work out the best way to test drive our coding in this area.</p>
<p>There seem to be 3 main ways that you can go about doing this, regardless of the testing framework you choose to you. We are using <a href="http://github.com/nkallen/screw-unit/tree/master">screw-unit</a> for our javascript testing.</p>
<h3>Mock everything out</h3>
<p>The idea here is that we mock out all calls made to jQuery functions and then we assert that the expected calls were made in our test.</p>
<p>Taking this approach means we are able to reduce the dependencies in our tests and they run very quickly. </p>
<p>The problem is that a lot of assertions become assertions checking that certain operations were called on the DOM since jQuery makes a lot of these type of calls in its code. The tests therefore end up being quite long and difficult to understand unless you were the one who initially wrote them.</p>
<p>We also effectively end up testing how the framework interacts with the DOM rather than testing our own code with this approach.</p>
<h3>Don&#8217;t mock anything</h3>
<p>The opposite approach is to just test directly against jQuery and then do assertions against the part of the DOM affected by the javascript code we are testing.</p>
<p>The problem here is that if you want to test against plugins which make ajax requests or carry out animation effects then the tests become dependent on how long these calls take and we need to find a way to block the test until those calls return which is quite difficult!</p>
<h3>Only stub certain calls</h3>
<p>The happy medium is that we test directly against the jQuery library but stub out ajax requests and animation effects. Our test assertions are against the state of the DOM to check that it was changed in the way that we expected.</p>
<p>This approach allows us to test our javascript code in terms of its behaviour without testing the internals of how jQuery works.</p>
<p>The wiring up of events to different controls on the page is done in our test but the actual logic that happens when these events are fired is in our js file under test.</p>
<p>We currently favour this last approach as it seems to give us the best of both worlds so to speak. It be would be interesting to hear how are other people going about Javascript testing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/01/24/jquery-approaches-to-testing/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>jQuery datepicker IE6 positioning bug</title>
		<link>http://www.markhneedham.com/blog/2009/01/06/jquery-datepicker-ie6-positioning-bug/</link>
		<comments>http://www.markhneedham.com/blog/2009/01/06/jquery-datepicker-ie6-positioning-bug/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 11:57:06 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ie6]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=814</guid>
		<description><![CDATA[We&#8217;ve been using the jQuery datepicker on my current project and came across some strange behaviour with regards to the positioning of the calendar in IE6. The calendar was always positioning itself right at the top of the screen instead of just below the textbox it was hooked up to but in Firefox it was [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been using the <a href="http://docs.jquery.com/UI/Datepicker">jQuery datepicker</a> on my current project and came across some strange behaviour with regards to the positioning of the calendar in IE6.</p>
<p>The calendar was always positioning itself right at the top of the screen instead of just below the textbox it was hooked up to but in Firefox it was working fine.</p>
<p>After a bit of exploration in the jQuery code (<a href="http://ui.jquery.com/latest/ui/ui.datepicker.js">ui.datepicker.js</a>) we worked out that the &#8216;document.documentElement.clientHeight&#8217; call in the &#8216;_checkOffset&#8217; function was always returning a value of 0 meaning that the position of the calendar was always right at the top of the screen.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">_checkOffset: function(inst, offset, isFixed) {
		var pos = inst.input ? this._findPos(inst.input[0]) : null;
		var browserWidth = window.innerWidth || document.documentElement.clientWidth;
		var browserHeight = window.innerHeight || document.documentElement.clientHeight;
...</pre></div></div>

<p>It turned that we were missing the doctype at the top of the HTML page which make the page standards compliant and as a result document.documentElement.clientHeight returns the proper height.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;	&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;</pre></div></div>

<p>Not having this set puts IE into Quirks Mode meaning that document.documentElement.clientHeight  always returns 0. </p>
<p>We found a post on the Webmaster World Forum which helps explain <a href="http://www.webmasterworld.com/forum21/11096.htm">why this change makes a difference</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/01/06/jquery-datepicker-ie6-positioning-bug/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Javascript: Creating quick feedback loops</title>
		<link>http://www.markhneedham.com/blog/2008/12/09/javascript-creating-quick-feedback-loops/</link>
		<comments>http://www.markhneedham.com/blog/2008/12/09/javascript-creating-quick-feedback-loops/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 11:13:21 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[feedback-loops]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=715</guid>
		<description><![CDATA[I&#8217;ve been working quite a lot with Javascript and in particular jQuery recently and since I haven&#8217;t done much in this area before all the tips and tricks are new to me. One thing which is always useful no matter the programming language is to use it in a way that you can get rapid [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working quite a lot with Javascript and in particular <a href="http://jquery.com/">jQuery</a> recently and since I haven&#8217;t done much in this area before all the tips and tricks are new to me.</p>
<p>One thing which is always useful no matter the programming language is to use it in a way that you can get rapid feedback on what you are doing.</p>
<p>Fortunately there are quite a few tools that allow us to do this with Javascript:</p>
<h3>Firebug</h3>
<p>The <a href="http://getfirebug.com/">Firefox plugin</a> is perhaps the quickest way of getting feedback on anything Javascript and indeed CSS related.</p>
<p>The ability to see which HTTP calls have been made on a page is invaluable for checking whether AJAX functionality is working correctly and DOM manipulation can be executed and tested on the fly.</p>
<p>Including jQuery in a page effectively makes Firebug the jQuery Interactive Console, allowing us to try out the different functions and see their effects immediately.</p>
<h3>Unit Testing Frameworks</h3>
<p>There are several javascript unit testing frameworks around at the moment which run in the browser and provide the ability to write assertions on our code.</p>
<p>I have been using <a href="http://docs.jquery.com/QUnit">QUnit</a> and <a href="http://github.com/nkallen/screw-unit/tree/master">screw-unit</a> and while they work reasonably well for simple tests, neither seems to be at the level of JUnit or NUnit for example. I&#8217;m sure this will come as they mature.</p>
<p>Other frameworks I&#8217;ve heard about but not tried: <a href="http://code.google.com/p/rhinounit/">RhinoUnit</a>, <a href="http://www.valleyhighlands.com/testingframeworks/">JSNUnit</a>, <a href="http://www.jsunit.net/">JSUnit</a>, no doubt there are others I haven&#8217;t heard about yet.</p>
<h3>Selenium IDE</h3>
<p>The <a href="http://seleniumhq.org/projects/ide/">sometimes forgotten Firefox plugin</a> is very useful for quickly creating repeatable scenarios to see the impact that code changes have had.</p>
<p>The beauty of this approach is that it takes out the manual steps in the process, so we can just make our changes and then re-run the test. The runner lights up green or red, taking out the need to manually verify the correctness of our assertions.</p>
<h3>Alert</h3>
<p>The <a href="http://www.mediacollege.com/internet/javascript/basic/alert.html">&#8216;alert&#8217;</a> function is perhaps most useful when we want to quickly verify the path being taken through a piece of code without having to step through it using the Firebug debugger.</p>
<p>It&#8217;s probably more useful for proving our assumptions than actual debugging and it&#8217;s certainly quick and easy to set up.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/12/09/javascript-creating-quick-feedback-loops/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery Validation &amp; Firefox Refresh Behaviour</title>
		<link>http://www.markhneedham.com/blog/2008/12/02/jquery-validation-firefox-refresh-behaviour/</link>
		<comments>http://www.markhneedham.com/blog/2008/12/02/jquery-validation-firefox-refresh-behaviour/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 12:54:52 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=688</guid>
		<description><![CDATA[We&#8217;ve been working quite a bit with jQuery and cross browser compatibility and one of the interesting differences we came across today was the behaviour of Firefox and Internet Explorer when it comes to refreshing a page. When you press refresh in Internet Explorer the page gets refreshed to the state that it was in [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been working quite a bit with <a href="http://jquery.com/">jQuery</a> and cross browser compatibility and one of the interesting differences we came across today was the behaviour of Firefox and Internet Explorer when it comes to refreshing a page.</p>
<p>When you press refresh in Internet Explorer the page gets refreshed to the state that it was in when you first loaded the URL, meaning that the state of the data in forms is returned to its original state.</p>
<p>Doing the same in Firefox doesn&#8217;t have the same behaviour &#8211; the page refreshes but it keeps the most recent data that was entered into the form. In theory this is quite nice behaviour because it means if you accidentally hit refresh it keeps all your data.</p>
<p>For us it was quite annoying as we had hooked up a <a href="http://docs.jquery.com/Plugins/Validation">validator</a> and some other custom code which declared certain parts of the page invalid if any data had changed from what was on the page when it first loaded.</p>
<p>The way we got around the problem was to fire the validation events on jQuery&#8217;s document ready as well as firing them on the change events for each of the form elements.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// validation code here as well as on change events for form elements</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Although it does mean that we are calling the validation in more places it has helped us to get around the problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/12/02/jquery-validation-firefox-refresh-behaviour/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

