<?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>Brian Grinstead &#187; loop</title>
	<atom:link href="http://www.briangrinstead.com/blog/tag/loop/feed" rel="self" type="application/rss+xml" />
	<link>http://www.briangrinstead.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 11 Jan 2012 20:23:16 +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>Keep Original Variable State Between Event Binding and Execution</title>
		<link>http://www.briangrinstead.com/blog/keep-original-variable-state-between-event-binding-and-execution</link>
		<comments>http://www.briangrinstead.com/blog/keep-original-variable-state-between-event-binding-and-execution#comments</comments>
		<pubDate>Wed, 15 Jul 2009 13:48:09 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[event binding]]></category>
		<category><![CDATA[event handling]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[variable scope]]></category>

		<guid isPermaLink="false">http://www.briangrinstead.com/blog/?p=291</guid>
		<description><![CDATA[Or: Binding Events Inside of a Loop with jQuery I wrote an article on the Foliotek Development Blog about saving the state of a variable inside a closure that is not executed immediately. For example, functions passed into event binding or setTimeout(). Here is a quick rundown of the problem and the solution (using the [...]]]></description>
			<content:encoded><![CDATA[<h3>Or: Binding Events Inside of a Loop with jQuery</h3>
<p>I <a href='http://www.foliotek.com/devblog/keep-variable-state-between-event-binding-and-execution/'>wrote an article on the Foliotek Development Blog</a> about saving the state of a variable inside a closure that is not executed immediately. For example, functions passed into event binding or setTimeout(). Here is a quick rundown of the problem and the solution (using the jQuery library). </p>
<h3>The Problem</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
	$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;body&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;&lt;ul id='container'&gt;&lt;/ul&gt;&quot;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;color: #0000FF;">for</span> <span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> i <span style="color: #339933;color: #000000;">=</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span> i <span style="color: #339933;color: #000000;">&lt;</span> <span style="color: #CC0000;color: #808080;">5</span><span style="color: #339933;color: #000000;">;</span> i<span style="color: #339933;color: #000000;">++</span><span style="color: #009900;color: #000000;">&#41;</span>
	<span style="color: #009900;color: #000000;">&#123;</span>
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> $item <span style="color: #339933;color: #000000;">=</span> $<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;&lt;li /&gt;&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;Item &quot;</span> <span style="color: #339933;color: #000000;">+</span> i<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;#container&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;color: #000000;">&#40;</span>$item<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
		$item.<span style="color: #660066;">click</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
			<span style="color: #000066;color: #0000FF;">alert</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;You clicked number &quot;</span> <span style="color: #339933;color: #000000;">+</span> i<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>  <span style="color: #006600; font-style: italic;color: #008000;">// always &quot;You clicked number 5&quot;</span>
		<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
	<span style="color: #009900;color: #000000;">&#125;</span>
<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span></pre></div></div>

<h3>The Solution</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
	$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;body&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;&lt;ul id='container'&gt;&lt;/ul&gt;&quot;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;color: #0000FF;">for</span> <span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> i <span style="color: #339933;color: #000000;">=</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span> i <span style="color: #339933;color: #000000;">&lt;</span> <span style="color: #CC0000;color: #808080;">5</span><span style="color: #339933;color: #000000;">;</span> i<span style="color: #339933;color: #000000;">++</span><span style="color: #009900;color: #000000;">&#41;</span>
	<span style="color: #009900;color: #000000;">&#123;</span>
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> $item <span style="color: #339933;color: #000000;">=</span> $<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;&lt;li /&gt;&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;Item &quot;</span> <span style="color: #339933;color: #000000;">+</span> i<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;#container&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;color: #000000;">&#40;</span>$item<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
		<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span> <span style="color: #006600; font-style: italic;color: #008000;">// Closure here here instead of &quot;bindItem()&quot;</span>
			<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> ind <span style="color: #339933;color: #000000;">=</span> i<span style="color: #339933;color: #000000;">;</span>
			$item.<span style="color: #660066;">click</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
				<span style="color: #000066;color: #0000FF;">alert</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;You clicked number &quot;</span> <span style="color: #339933;color: #000000;">+</span> ind<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span> <span style="color: #006600; font-style: italic;color: #008000;">// Works as expected</span>
			<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span> <span style="color: #006600; font-style: italic;color: #008000;">// Execute immediately</span>
	<span style="color: #009900;color: #000000;">&#125;</span>
<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span></pre></div></div>

<p>The solution uses an immediately executing function to create a new scope and declare a variable &#8220;ind&#8221; that is reserved a new space in memory instead of simply referencing &#8220;i&#8221;. Check out the <a href='http://www.foliotek.com/devblog/keep-variable-state-between-event-binding-and-execution/'>full article</a> for more details.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briangrinstead.com/blog/keep-original-variable-state-between-event-binding-and-execution/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

