
<?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>Cory Schires &#187; jQuery</title>
	<atom:link href="http://coryschires.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://coryschires.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 14 Feb 2012 22:45:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>JQuery Quote Rotator Plugin.</title>
		<link>http://coryschires.com/jquery-quote-rotator-plugin/</link>
		<comments>http://coryschires.com/jquery-quote-rotator-plugin/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 03:58:12 +0000</pubDate>
		<dc:creator>Cory Schires</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://coryschires.com/?p=265</guid>
		<description><![CDATA[Here’s a jQuery plugin that rotates through list items with a nice fade effect...]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a jQuery plugin that rotates through list items with a nice fade effect. You may nest any type of element you like inside a given list item.</p>
<p><a href="http://coryschires.github.com/quote-rotator/" class="new_window no_underline"><br />
<img class="blog_post_banner msg" title="Click to view demo." src="http://coryschires-blog-post-images.s3.amazonaws.com/quote_rotator_poster.png" alt="Quote Rotator Demo" /><br />
</a></p>
<h3>How to make it work.</h3>
<p>Drop these scripts in your header:</p>
<div class="code_block">
<pre class="brush: ruby;">
   &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt;
   &lt;script src="js/jquery.quote_rotator.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
</pre>
</div>
<p>Call the function to any list element:</p>
<div class="code_block">
<pre class="brush: jscript;">
        $(document).ready(function() {
            $('ul#quotes').quote_rotator();
        });</pre>
</div>
<p>You can specify which quote you’d like to show first by adding a class of active to the list item. If you don’t explicitly add an active class, it will default to the first item in the list.</p>
<h3>Configuration options.</h3>
<div class="code_block">
<pre class="brush: jscript;">
        $('ul#quotes').quote_rotator({
         rotation_speed: 7000,                    // defaults to 5000
         pause_on_hover: false,                   // defaults to true
         randomize_first_quote: true              // defaults to false
         buttons: { next: '>>', previous: '<<' }  // defaults to false
        });</pre>
</div>
<p>Rotation will pause on mouse hover by default. This is nice if you're rotating through links or other elements that you might not want fading away from the user.</p>
<p>Also notice that next/previous buttons are not included by default. If you want buttons, you need override the <span class="code_text">buttons</span> option. As in the example above, assign a javascript object with <span class="code_text">next</span> and <span class="code_text">previous</span> keys. The values will be used as text for your buttons.</p>
<p><a class="new_window download" href="http://github.com/coryschires/quote-rotator">Download the project from github</a></p>
]]></content:encoded>
			<wfw:commentRss>http://coryschires.com/jquery-quote-rotator-plugin/feed/</wfw:commentRss>
		<slash:comments>63</slash:comments>
		</item>
		<item>
		<title>Introducing Image Mapper!</title>
		<link>http://coryschires.com/introducing-image-mapper/</link>
		<comments>http://coryschires.com/introducing-image-mapper/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 03:39:27 +0000</pubDate>
		<dc:creator>Cory Schires</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://coryschires.com/?p=241</guid>
		<description><![CDATA[Image Mapper is a web application that makes it dead easy to create HTML image maps...]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working hard on a new app for the past few days &#8211; <a href="http://image-mapper.com">Image Mapper</a>. It makes creating HTML image maps super easy.</p>
<p><a href="http://www.image-mapper.com/" class="new_window no_underline"><br />
<img class="msg_small_font" title="Click to see image mapper." src="http://coryschires-blog-post-images.s3.amazonaws.com/image_mapper_preview.png" alt="Image Mapper" /><br />
</a></p>
<p>If you&#8217;ve ever used DreamWeaver to create an image map, then you&#8217;ll be pretty familiar with image-mapper&#8217;s interface. It&#8217;s three dead simple steps:</p>
<ol>
<li>Upload your image</li>
<li>Map your image</li>
<li>Copy your code</li>
</ol>
<p>It all happens on a single page so it&#8217;s pretty hard to get confused.</p>
<p>It&#8217;s made with Ruby on Rails and jQuery. And best of all, no more having to deal with DreamWeaver! Have a look for yourself.</p>
<p>Visit <a href="http://image-mapper.com">Image Mapper</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://coryschires.com/introducing-image-mapper/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JQuery hover caption plugin.</title>
		<link>http://coryschires.com/jquery-hover-caption-plugin/</link>
		<comments>http://coryschires.com/jquery-hover-caption-plugin/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 23:13:42 +0000</pubDate>
		<dc:creator>Cory Schires</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://coryschires.com/?p=202</guid>
		<description><![CDATA[Here’s a simple jQuery plugin that adds both a caption and a gray-out effect when hovering over an image...]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a simple jQuery plugin that adds both a caption and a gray-out effect when hovering over an image. For example:</p>
<p><img class="msg" title="This is the title attribute." alt="Flapper 1922" src="http://coryschires-blog-post-images.s3.amazonaws.com/flapper_1922.jpg"/></p>
<h3>How to make it work.</h3>
<p>1. Drop these scripts in your header:</p>
<div class="code_block">
<pre class="brush: ruby;">
		&lt;script src="js/jquery-1.4.1.min.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
		&lt;script src="js/jquery.hover_caption.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
	</pre>
</div>
<p>2. Call the plugin function on your images:</p>
<div class="code_block">
<pre class="brush: jscript;">
		$(document).ready(function() {
			$('img').hover_caption();
		});
    </pre>
</div>
<p>3. Add this tiny bit of CSS somewhere:</p>
<div class="code_block">
<pre class="brush: css;">
		.hover_caption {
			background-image: url(hover_caption_bg.png);
			/* NOTE: if you're img elements have paddings
			or margins you'll need to match them here
			to get things lined up properly. */
		}
    </pre>
</div>
<p>And here&#8217;s some configuration options:</p>
<div class="code_block">
<pre class="brush: jscript;">
		$('img').hover_caption({
			caption_font_size: '18px',
			caption_color: 'white',
			caption_bold: true,
			caption_default: "Click for screenshots."
		});
	</pre>
</div>
<h3>And the captions are pretty smart.</h3>
<p>If your image has a title attribute, the plugin will use that as the caption. Otherwise it will default to <span class="code_text">Click for screenshots</span>. You can change the default if you like, of course.</p>
<h3>A few gotchas.</h3>
<ul>
<li>Be sure the path to <span class="code_text">hover_caption_bg.png</span> is correct.</li>
<li>Don&#8217;t forget to add <span class="code_text">hover_caption_bg.png</span> to your project.</li>
<li>Be sure the path to <span class="code_text">hover_caption.js</span> is correct.</li>
<li>Make sure you&#8217;ve referenced jQuery first in your header.</li>
</ul>
<p><a class="new_window download" href="http://github.com/coryschires/hover-caption">Download the project from github</a></p>
]]></content:encoded>
			<wfw:commentRss>http://coryschires.com/jquery-hover-caption-plugin/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Using scoped selectors in jQuery.</title>
		<link>http://coryschires.com/using-scoped-selectors-in-jquery/</link>
		<comments>http://coryschires.com/using-scoped-selectors-in-jquery/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 02:15:33 +0000</pubDate>
		<dc:creator>Cory Schires</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://coryschires.com/?p=168</guid>
		<description><![CDATA[Have you ever seen a selector that looks like this <span class="code_text">$('p.featured', this)</span>? This is a scoped selector. Here's how they work...]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve probably seen a selector written like this:</p>
<div class="code_block">
<pre class="brush: jscript;">
		var stuff_on_sale = $('div#sale_items');
		$('p.featured', stuff_on_sale);
    </pre>
</div>
<p>And, if you&#8217;re like me, you might have found this syntax confusing. JQuery targets elements using CSS selectors &#8211; and this second line definitely is not a CCS selector. So what&#8217;s going on here?</p>
<h3 class="vag_post_header">Introducing the scoped selector.</h3>
<p>The first argument (e.g. <span class="code_text">p.featured</span>) is a standard CSS selector. The second argument (e.g. <span class="code_text">this</span>) could be any jQuery object. </p>
<p>This selector then will return any elements that are both (a) matching the first argument and (b) contained within the scope of the second argument. </p>
<p>So, in our example, the selector would return all paragraphs with a class of featured contained within the the div of sale items. </p>
<h3 class="vag_post_header">Hol&#8217; up mayne!</h3>
<p>You&#8217;re probably thinking couldn&#8217;t I just do this instead?</p>
<div class="code_block">
<pre class="brush: jscript;">
		$('div#sale_items p.featured');
	</pre>
</div>
<p>Yes. You could and whenever possible you should. But there are some instances when targeting a specific element within the context of another set of elements can be very handy. </p>
<p>For example, I use scoped selectors frequently when:</p>
<ul>
<li>Iterating through arrays elements</li>
<li>Accomplishing complex tasks with a click function</li>
<li>Organizing large chunks of code &#8211; like plugins</li>
</ul>
<p>And that&#8217;s just a few uses. Once you&#8217;re familiar with the idea, you&#8217;ll be scoping selectors all over the place.</p>
]]></content:encoded>
			<wfw:commentRss>http://coryschires.com/using-scoped-selectors-in-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to add selected states to your navigation using jQuery.</title>
		<link>http://coryschires.com/how-to-add-selected-states-to-your-navigation-using-jquery/</link>
		<comments>http://coryschires.com/how-to-add-selected-states-to-your-navigation-using-jquery/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 04:47:07 +0000</pubDate>
		<dc:creator>Cory Schires</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://coryschires.com/?p=158</guid>
		<description><![CDATA[Here’s a simple jQuery solution for adding selected states to your navigation...]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a simple jQuery solution for adding selected states to your navigation.</p>
<p>The sample HTML looks like this:</p>
<div class="code_block">
<pre class="brush: ruby;">
<ul id="navigation">
<li><a href="home.html">Home</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="about.html">About</a></li>
<li><a href="information.html">Info</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</pre>
</div>
<p>And the sample CSS:</p>
<div class="code_block">
<pre class="brush: css;">
		li {
			background-color: white;
			color: black;
			padding: 5px 10px;
		}
		li.selected {
			background-color: black;
			color: white;
		}
	</pre>
</div>
<p>And the corresponding jQuery:</p>
<div class="code_block">
<pre class="brush: jscript;">
		$(document).ready(function() {

			// store url for current page as global variable
			current_page = document.location.href

			// apply selected states depending on current page
			if (current_page.match(/home/)) {
				$("ul#navigation li:eq(0)").addClass('selected');
			} else if (current_page.match(/services/)) {
				$("ul#navigation li:eq(1)").addClass('selected');
			} else if (current_page.match(/about/)) {
				$("ul#navigation li:eq(2)").addClass('selected');
			} else if (current_page.match(/information/)) {
				$("ul#navigation li:eq(3)").addClass('selected');
			} else if (current_page.match(/contact/)) {
				$("ul#navigation li:eq(4)").addClass('selected');
			} else { // don't mark any nav links as selected
				$("ul#navigation li").removeClass('selected');
			};

		});
    </pre>
</div>
<h3 class="vag_post_header">How does it work?</h3>
<p>First we capture the url of the current page in a variable. Then we use that variable to write conditions based on a regular expression (e.g. if the current url contains the word &#8216;about&#8217;). Finally, we use the <span class="code_text">:eq(index)</span> selector to target the corresponding <span class="code_text">&lt;li&gt;</span> and add a class of selected.</p>
<p>The only thing that&#8217;s remotely tricky here is the <span class="code_text">:eq(index)</span> selector. This allows you to use an index to target a specific element within a larger array of matched elements. </p>
<p>So in our example, <span class="code_text">ul#navigation li</span> would return an array of all five <span class="code_text">&lt;li&gt;</span> elements whereas <span class="code_text">ul#navigation li:eq(4)</span> would return only the fifth <span class="code_text">&lt;li&gt;</span>. This saves us the trouble of having to apply a unique id to each list item.</p>
<h3 class="vag_post_header">A few gotchas.</h3>
<ul>
<li>
		Make sure your regular expression matchers are not too general or you might end up adding selected states to the wrong pages. For example, this might not be a good solution for a blog where the slugs correspond to post tiles.
	</li>
<li>
		Make sure to include the most recent version of jQuery in your head element.
	</li>
<li>
		Make sure jQuery is loaded before the script for your selected navigation.
	</li>
<li>
		Don&#8217;t forget to place your script inside the document ready function. Otherwise your script will run before the DOM is fully loaded &#8211; and do nothing.
	</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://coryschires.com/how-to-add-selected-states-to-your-navigation-using-jquery/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Making jQuery and Rails play nice with AJAX.</title>
		<link>http://coryschires.com/making-jquery-and-rails-play-nice-with-ajax/</link>
		<comments>http://coryschires.com/making-jquery-and-rails-play-nice-with-ajax/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 02:00:41 +0000</pubDate>
		<dc:creator>Cory Schires</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby-on-Rails]]></category>

		<guid isPermaLink="false">http://coryschires.com/?p=128</guid>
		<description><![CDATA[Rails has built in support for prototype but now most of us are using jQuery. I'll show you how to get Rails and jQuery working together with AJAX...]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to observe for a client-side event (click, drag-n-drop, column resize, etc.) and store the result in your database. For example, say your application has customizable profile pages where users can rearrange, add, remove, and resize widgets (e.g. stock ticker, twitter feed, etc.).</p>
<p>Now, jQuery makes this functionality super easy &#8211; just a few lines of code. But here&#8217;s the tricky part: the configuration of each profile page needs to be stored in your database and be associated with a given user. Otherwise users would lose all their changes on page refresh. No good.</p>
<p>This means every time a user changes something (e.g. removes a stock ticker) you need to let rails know the profile has been changed so that the appropriate columns can be updated in your database (e.g. set the ticker&#8217;s &#8216;hidden&#8217; boolean to true). And ideally, this should all happen silently in the background so that the user&#8217;s not even aware their changes are being saved on the fly.</p>
<p>Now, we&#8217;re talking about making the client talk to the server without a page refresh. That means AJAX. So here&#8217;s the plan&#8230;</p>
<h3 class="vag_post_header">1. Create your javascript event.</h3>
<p>For simplicity, say you&#8217;d like to let the user can change the background color of their profile page. Let&#8217;s give them three buttons to toggle between red, yellow, and purple. Your html would look like this:</p>
<div class="code_block">
<pre class="brush: ruby;">
&lt;body&gt;
<div id="green" class="change_bg_color">Make Green</div>
<div id="yellow" class="change_bg_color">Make Yellow</div>
<div id="purple" class="change_bg_color">Make Purple</div>

&lt;/body&gt;
</pre>
</div>
<p>And your jQuery would look like this:</p>
<div class="code_block">
<pre class="brush: jscript;">
$('.change_bg_color').click(function() {
	if (this.id == 'green') { $('body').css('background-color', 'green') };
	if (this.id == 'yellow') { $('body').css('background-color', 'yellow') };
	if (this.id == 'purple') { $('body').css('background-color', 'purple') };
});
</pre>
</div>
<p>That will get our background changing color, but as soon as the page refreshes we&#8217;ll lose our changes. So let&#8217;s get rails involved.</p>
<h3 class="vag_post_header">2. Set up the appropriate models and associations</h3>
<p>I&#8217;ll move pretty quickly here cause it&#8217;s so easy. I assume you have a current_user stored in your session. I&#8217;ll also assume you&#8217;ve set up a one-to-one association between &#8216;users&#8217; and &#8216;profile_settings.&#8217; This means you should be able to do something like this in your controller:</p>
<div class="code_block">
<pre class="brush: ruby;">def make_background_green
	current_user.profile_setting.update_attributes(:background_color =&gt; 'green')
end
</pre>
</div>
<p>And then you can apply this color using inline style:</p>
<div class="code_block">
<pre class="brush: ruby;">
&lt;body style="background-color=&lt;%= current_user.profile_setting.background_color %&gt;;" &gt;
<div id="green" class="change_bg_color">&lt;%= link_to "Make Green", :action =&gt; "make_background_green" %&gt;</div>
<div id="yellow" class="change_bg_color">&lt;%= link_to "Make Yellow", :action =&gt; "make_background_yellow" %&gt;</div>
<div id="purple" class="change_bg_color">&lt;%= link_to "Make Purple", :action =&gt; "make_background_purple" %&gt;</div>

&lt;/body&gt;
</pre>
</div>
<p>Of course this is just nasty for a couple of reasons: (a) you&#8217;d have to write a new method for each possible color, (b) the markup is sort of ugly, and (c) we&#8217;re relying on a page refresh to do the work. So let&#8217;s clean this up a bit using AJAX.</p>
<h3 class="vag_post_header">3. Update your profile settings via AJAX.</h3>
<p>First, let&#8217;s get our html looking better:</p>
<div class="code_block">
<pre class="brush: html;">
&lt;body style="background-color=&lt;%= current_user.profile_setting.background_color %&gt;;" &gt;
<div id="green" class="change_bg_color">Make Green</div>
<div id="yellow" class="change_bg_color">Make Yellow</div>
<div id="purple" class="change_bg_color">Make Purple</div>

&lt;/body&gt;
</pre>
</div>
<p>Then lets add some AJAX to our click event:</p>
<div class="code_block">
<pre class="brush: jscript;">$('h3').click(function() {
	if (this.id == 'green') { $('body').css('background-color', 'green') };
	if (this.id == 'yellow') { $('body').css('background-color', 'yellow') };
	if (this.id == 'purple') { $('body').css('background-color', 'purple') };		

	$.post('http://your_site.com/profile_controller/change_background_color', {
		'background_color' : this.id
	});
});</pre>
</div>
<p>Here&#8217;s where you need to pay attention. We&#8217;re sending an AJAX post request with two arguments: (1) the url we&#8217;d like to load and (2) the data we&#8217;d like to pass along as a key/value pair. This means your controller is going to fire the &#8216;change_background_color&#8217; action and will have access to the params[:background_color] with a value equal to the clicked element&#8217;s id.</p>
<p>And so your controller will look like this:</p>
<div class="code_block">
<pre class="brush: ruby;">
def change_background_color
  current_user.profile_setting.update_attributes(:background_color =&gt; params[:background_color])
  render :nothing =&gt; true  # make sure the action knows not to load a template
end
</pre>
</div>
<h3 class="vag_post_header">4. Mission accomplished.</h3>
<p>Your profile settings should now be updating quietly in the background. If you have any problems, start up firebug and watch your AJAX hit register in the console log. This can be very helpful when debugging. But don&#8217;t get discouraged. Once you get this process down, all sorts of cool things become possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://coryschires.com/making-jquery-and-rails-play-nice-with-ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

