<?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>Blog from the Loft &#124; PK Shiu Personal Blog &#187; jquery</title>
	<atom:link href="http://www.pkshiu.com/loft/archive/tag/jquery/feed" rel="self" type="application/rss+xml" />
	<link>http://www.pkshiu.com/loft</link>
	<description>Thoughts without walls</description>
	<lastBuildDate>Sun, 05 Feb 2012 01:50:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Understanding this, $(this), and event in a JQuery callback function</title>
		<link>http://www.pkshiu.com/loft/archive/2009/01/understanding-this-this-and-event-in-a-jquery-callback-function</link>
		<comments>http://www.pkshiu.com/loft/archive/2009/01/understanding-this-this-and-event-in-a-jquery-callback-function#comments</comments>
		<pubDate>Thu, 01 Jan 2009 16:12:56 +0000</pubDate>
		<dc:creator>pk</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.pkshiu.com/loft/?p=656</guid>
		<description><![CDATA[I run into this issue all the time. Inside a callback function, like a function that response to OnClick, do I use this, $(this), or event to get to what I need? Let&#8217;s use a real example to demostrate what to do: A web page of biography for a website uses a side menu to [...]]]></description>
			<content:encoded><![CDATA[<p>I run into this issue all the time. Inside a callback function,<br />
like a function that response to OnClick, do I use <code>this</code>,<br />
<code>$(this)</code>, or <code>event</code> to get to what I need?</p>
<p>Let&#8217;s use a real example to demostrate what to do: A web page of<br />
biography for a website uses a side menu to select one of several bios<br />
to display in the main area. Doing this in JQuery means that we want<br />
to hook a handler to the click events of each of the &lt;A&gt; element<br />
in the menu. When a menu item is clicked, we will display the<br />
corresponding biography enclosed in a &lt;DIV&gt;</p>
<h3>HTML</h3>
<p>Here&#8217;s the HTML for the menu area and the biography area.<br />
I am using ID&#8217;s to distinguish each biography.</p>
<p>Line 1 gives the menu a class of MENU so that we can<br />
easily find it in JQuery.<br />
Line 2 to 4 provide three menu items for the user. Notice<br />
that at line 3 and 4, there are extra styling for the menu text.</p>
<p>Line 7 and on are three DIV&#8217;s with ID corresponding to each<br />
of the menu items. Each DIV has a class of BIO, again for<br />
easy selection by JQuery.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;ul&gt; class=&quot;menu&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#&quot; id=&quot;1&quot;&gt;1. Click me to show first bio.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot; id=&quot;2&quot;&gt;2. Click &lt;em&gt;me&lt;/em&gt; to show &lt;em&gt;second&lt;/em&gt; bio.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot; id=&quot;3&quot;&gt;3. Click &lt;strong&gt;me to show third&lt;/strong&gt; bio.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;bio&quot; id=&quot;1&quot;&gt;
Biography for person number one.
&lt;/div&gt;
&lt;div class=&quot;bio&quot; id=&quot;2&quot;&gt;
Biography for person number two.
&lt;/div&gt;
&lt;div class=&quot;bio&quot; id=&quot;3&quot;&gt;
Biography for person number three.
&lt;/div&gt;
</pre>
<h3>JQuery: Initialization</h3>
<p>The following initialization code first hides all biography divs on load,<br />
and then show just the first one by default. Notice how we use the<br />
<code>:first</code> selector to make selecting the first matching<br />
DIV easy.</p>
<p>Line 9 attach our function <code>click_me</code> to the menu.</p>
<pre class="brush: jscript; title: ; notranslate">
$(function(){

    /* Hide all bio divs, then show the very first one. */
    $(&quot;.bio&quot;).hide();
    $(&quot;.bio:first&quot;).show();

    /* Hook the onClick function, any A inside an object with
       class menu (UL in this case) */
    $(&quot;.menu A&quot;).click(click_me);

});
</pre>
<h3>JQuery Handler Function</h3>
<p>Here is the handler function that we hooked to the anchors in<br />
the menu. Can you guess what&#8217;s the console log output would be?</p>
<pre class="brush: jscript; title: ; notranslate">
function click_me(event){

    console.log('clicked');
    /* &quot;this&quot; is DOM element attached by the function */
    console.log('this=' + this);

    /* &quot;event.target&quot; is DOM element receiving the event, can be a child */
    console.log('event.target=' + event.target);

    /* Show the bio selected */
    var s = &quot;.bio[id=&quot; + this.id + &quot;]&quot;;
    $(&quot;.bio&quot;).hide();
    $(s).show();

    /* Silly effect to show the use of $(this). */
    $(this).fadeOut();
    $(this).fadeIn();

};
</pre>
<p>Here is the explanation:<br />
1. <code>this</code> inside a JQuery event handler is the<br />
DOM element that has the handler attached. This (sic) this is<br />
not necessarily the A link itself. If we have attached the<br />
handler to something else, like the containing DIV, we will<br />
get the DIV DOM element doesn&#8217;t matter which inside elements<br />
of the DIV we clicked. In this case we have attached the <code>click_me</code><br />
function to the anchors (A) themselves, so we are safe<br />
to know that <code>this.id</code> will give us the ID specified<br />
within our A tags.</p>
<p>2. The standard argument passed to the function, <code>event</code><br />
is the <a href="http://docs.jquery.com/Events_(Guide)">eventObject</a>;<br />
It is a JQuery structure the contains many useful attribute regarding<br />
the (click) event. Specifically event.target is the DOM element received the click event.<br />
Note that it will be &#8220;the DOM element that issued the event. This can be the element that registered for the event or a child of it&#8221;.</p>
<p>For example, in menu item two, if you clicked on the word &#8220;me&#8221;, it will<br />
be the span element, <strong>not</strong> the A element.</p>
<p>3. Finally, when you turn <code>this</code> into <code>$(this)</code>,<br />
you are creating a JQuery object out of, well, <code>this</code>.<br />
Turning a DOM element (the A in this case) into a JQuery object<br />
allows you to use all the JQuery functions on it. We do not really<br />
need to do that here, but just as a demonstration, we use <code>$(this)</code><br />
to fade the menu item out and in. We need to turn the A into a<br />
JQuery object so that we can call the JQuery <code>fadeOut</code><br />
and <code>fadeIn</code> functions.</p>
<p>You can see a working version of <a href="http://www.pkshiu.com/example/jquery_example_01.html">this example here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pkshiu.com/loft/archive/2009/01/understanding-this-this-and-event-in-a-jquery-callback-function/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>JQuery Cheatsheet</title>
		<link>http://www.pkshiu.com/loft/archive/2008/09/jquery-cheatsheet</link>
		<comments>http://www.pkshiu.com/loft/archive/2008/09/jquery-cheatsheet#comments</comments>
		<pubDate>Tue, 02 Sep 2008 15:02:48 +0000</pubDate>
		<dc:creator>pk</dc:creator>
				<category><![CDATA[technology]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.pkshiu.com/loft/archive/2008/09/jquery-cheatsheet</guid>
		<description><![CDATA[JQuery is useful, but it is built on top of the idiosyncratic javascript and DOM model. I find the whole environment lack consistency. So here are some quick cookbook style notes on how to do specific things: Clear all checkboxes in a form $(&#8220;:checkbox&#8221;).attr(&#8220;checked&#8221;,false) Technorati Tags: jquery]]></description>
			<content:encoded><![CDATA[<p>JQuery is useful, but it is built on top of the idiosyncratic javascript and DOM model. I find the whole environment lack consistency. So here are some quick cookbook style notes on how to do specific things:</p>
<p>Clear all checkboxes in a form</p>
<p>$(&#8220;:checkbox&#8221;).attr(&#8220;checked&#8221;,false)</p>
<p><!-- technorati tags start --></p>
<p style="text-align:right;font-size:10px;">Technorati Tags: <a rel="tag" href="http://www.technorati.com/tag/jquery">jquery</a></p>
<p><!-- technorati tags end --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pkshiu.com/loft/archive/2008/09/jquery-cheatsheet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

