<?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>Jetlogs.org &#187; jQuery</title>
	<atom:link href="http://jetlogs.org/category/web-development/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://jetlogs.org</link>
	<description>Web development, tutorials, video games, papercraft, and technology</description>
	<lastBuildDate>Thu, 10 Jun 2010 05:57:34 +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>Filtering Lists With Multiple Categories Using jQuery</title>
		<link>http://jetlogs.org/2009/04/03/filtering-lists-with-multiple-categories-using-jquery/</link>
		<comments>http://jetlogs.org/2009/04/03/filtering-lists-with-multiple-categories-using-jquery/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 03:55:17 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://jetlogs.org/?p=778</guid>
		<description><![CDATA[This article is a continuation to the Filtering Lists Using jQuery. In the previous article, we&#8217;ve discussed how to filter a simple list using a single category. However, in real life this isn&#8217;t simply the case. What if we have a new column for our database table which adds a new category to our fruits [...]]]></description>
			<content:encoded><![CDATA[<p>This article is a continuation to the <a href="http://jetlogs.org/2009/02/10/filtering-lists-using-jquery/">Filtering Lists Using jQuery</a>. In the previous article, we&#8217;ve discussed how to filter a simple list using a single category. However, in real life this isn&#8217;t simply the case. What if we have a new column for our database table which adds a new category to our fruits list:</p>
<p><b>tbl_fruits</b></p>
<table width=300 cellspacing=0 cellpadding=2 border=1>
<tr style="background-color:#ccf">
<td><b>fruit</b></td>
<td><b>color</b></td>
<td><b>type</b></td>
</tr>
<tr>
<td>Apple</td>
<td>Red</td>
<td>Fruit</td>
</tr>
<tr>
<td>Grape</td>
<td>Blue</td>
<td>Fruit</td>
</tr>
<tr>
<td>Lemon</td>
<td>Yellow</td>
<td>Fruit</td>
</tr>
<tr>
<td>Cherry</td>
<td>Red</td>
<td>Fruit</td>
</tr>
<tr>
<td>Banana</td>
<td>Yellow</td>
<td>Fruit</td>
</tr>
<tr>
<td>Strawberry</td>
<td>Red</td>
<td>Berry</td>
</tr>
<tr>
<td>Blueberry</td>
<td>Blue</td>
<td>Berry</td>
</tr>
<tr>
<td>Raspberry</td>
<td>Red</td>
<td>Berry</td>
</tr>
<tr>
<td>Pineapple</td>
<td>Yellow</td>
<td>Fruit</td>
</tr>
<tr>
<td>Yellowberry</td>
<td>Yellow</td>
<td>Berry</td>
</tr>
</table>
<p>The front-end would be a products list with all of the products in place and customers can filter which color and type of fruits they need to find.</p>
<p>Traditional wisdom would tell us that we would need to make a script that would send a query to the backend for every post request the reload the page for the search results.</p>
<p><span class="code">SELECT fruit FROM `tbl_fruits` WHERE color = &#8220;Red&#8221; AND type = &#8220;Berry&#8221;;</span></p>
<p>However there are some instances that this approach has too much overhead on our backend and our database. In cases where all data has already been preloaded, we don&#8217;t have to query the database once again for data that is already available. We can simply use jQuery to control this information.<br />
<span id="more-778"></span></p>
<p>First of all, we need to make our form. Lets make 2 select boxes for our category selection:</p>
<pre><code class="html">Select Color Category:
&lt;select name="selFruitColor" id="fruitColor"&gt;
	&lt;option value="all"&gt;Any Color&lt;/option&gt;
	&lt;option value="red"&gt;Red&lt;/option&gt;
	&lt;option value="blue"&gt;Blue&lt;/option&gt;
	&lt;option value="yellow"&gt;Yellow&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
Select Fruit Type:
&lt;select name="selFruitType" id="fruitType"&gt;
	&lt;option value="all"&gt;Any Type&lt;/option&gt;
	&lt;option value="fruit"&gt;Fruit&lt;/option&gt;
	&lt;option value="berry"&gt;Berry&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;input type="button" id="btnFilter" value="Filter the list"&gt;</code></pre>
<p>Thing to note:<br />
<b><span class="code">#fruitColor</span></b> &#8211; The id of the select box for choosing fruit color<br />
<b><span class="code">#fruitType</span></b> &#8211; The id of the select box for choosing fruit type<br />
<b><span class="code">#btnFilter</span></b> &#8211; The id of the button that triggers the filtering of our list</p>
<p>Labelling the <span class="code">id</span> of our elements is quite important so we can refer to them later in our script for triggering events and extracting values.</p>
<p>Now for displaying our data, in this case, we need to label each item properly to match with their respective categories. We can do this either manually for static pages or dynamically using your script of choice:</p>
<pre><code class="html">&lt;ul&gt;
	&lt;li class="item red fruit"&gt;Apple&lt;/li&gt;
	&lt;li class="item blue fruit"&gt;Grape&lt;/li&gt;
	&lt;li class="item yellow fruit"&gt;Lemon&lt;/li&gt;
	&lt;li class="item red fruit"&gt;Cherry&lt;/li&gt;
	&lt;li class="item yellow fruit"&gt;Banana&lt;/li&gt;
	&lt;li class="item red berry"&gt;Strawberry&lt;/li&gt;
	&lt;li class="item blue berry"&gt;Blueberry&lt;/li&gt;
	&lt;li class="item red berry"&gt;Raspberry&lt;/li&gt;
	&lt;li class="item yellow fruit"&gt;Pineapple&lt;/li&gt;
	&lt;li class="item yellow berry"&gt;Yellowberry&lt;/li&gt;
&lt;/ul&gt;</code></pre>
<p>Thing to note:<br />
<b><span class="code">.item</span></b> &#8211; This is the class of all the items from the list. This is important so we can show all, or ignore one of the categories when this is selected.<br />
<b><span class="code">.red.berry, .blue.fruit, .yellow.fruit</span></b> &#8211; Depending on the fruit&#8217;s category, we will apply the appropriate class. For example, an item under blue and berry will have class=&#8221;item blue berry&#8221;</p>
<p>And finally to piece it all together, we would need the jQuery script to filter our list based on our trigger:</p>
<pre><code class="javascript">$(document).ready(function()
{
	$("#btnFilter").click(function()
	{
		var fruitColor	= $("#fruitColor").val();
		var fruitType	= $("#fruitType").val();
		var fruitColorSelector = '';
		var fruitTypeSelector = '';		

		if (fruitColor == "all" &#038;&#038; fruitType == "all")
		{
			//show all items
			$(".item").show();
		}
		else
		{
			if (fruitType != "all")
			{
				fruitTypeSelector = '.' + fruitType
			}

			if (fruitColor != "all")
			{
				fruitColorSelector = '.' + fruitColor
			}

			$(".item").hide();
			$(fruitTypeSelector + fruitColorSelector).show();
		}

	});

});</code></pre>
<p>So what does this script basically do?</p>
<p>When <b><span class="code">#btnFilter</span></b> has been clicked, it takes the value of <b><span class="code">#fruitType</span></b> and <b><span class="code">#fruitColor</span></b>. If it detects that both categories are set to &#8220;any&#8221;, it will display everything on the list.</p>
<p>If the categories for fruitType and fruitColor and both set, it will only display items in the list that fits the class criteria of both.</p>
<p>Now if it detects that just one of the categories is set to &#8220;any&#8221;, it sets the selector for the category to an empty string to the category will be omitted. Only one of the categories will then take effect.</p>
<p>A working demo of this article can be found below:<br />
<a href="http://jetlogs.org/jquery/jquery_list_filter_multiple.html" target="_blank">jQuery: List Filter Multiple Categories Demo</a></p>
<p>The source code for the demo can be downloaded here:<br />
<a href="http://jetlogs.org/jquery/jquery_list_filter_multiple.zip">jQuery: List Filter Multiple Categories Demo Source Code</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2009%2F04%2F03%2Ffiltering-lists-with-multiple-categories-using-jquery%2F&amp;title=Filtering+Lists+With+Multiple+Categories+Using+jQuery" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2009%2F04%2F03%2Ffiltering-lists-with-multiple-categories-using-jquery%2F&amp;title=Filtering+Lists+With+Multiple+Categories+Using+jQuery" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2009%2F04%2F03%2Ffiltering-lists-with-multiple-categories-using-jquery%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2009%2F04%2F03%2Ffiltering-lists-with-multiple-categories-using-jquery%2F&amp;title=Filtering+Lists+With+Multiple+Categories+Using+jQuery" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2009%2F04%2F03%2Ffiltering-lists-with-multiple-categories-using-jquery%2F&amp;title=Filtering+Lists+With+Multiple+Categories+Using+jQuery" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2009%2F04%2F03%2Ffiltering-lists-with-multiple-categories-using-jquery%2F&amp;title=Filtering+Lists+With+Multiple+Categories+Using+jQuery" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2009%2F04%2F03%2Ffiltering-lists-with-multiple-categories-using-jquery%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2009%2F04%2F03%2Ffiltering-lists-with-multiple-categories-using-jquery%2F&amp;t=Filtering+Lists+With+Multiple+Categories+Using+jQuery" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2009/04/03/filtering-lists-with-multiple-categories-using-jquery/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Filtering Lists Using jQuery</title>
		<link>http://jetlogs.org/2009/02/10/filtering-lists-using-jquery/</link>
		<comments>http://jetlogs.org/2009/02/10/filtering-lists-using-jquery/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 05:42:40 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://jetlogs.org/?p=751</guid>
		<description><![CDATA[List filtering is when we have a list of different items, belonging to different categories and we can choose a category to hide or display. This is very convenient for users if they only want to view items belonging only to a particular category. However, list filtering can become one of the most resource-intensive workloads [...]]]></description>
			<content:encoded><![CDATA[<p>List filtering is when we have a list of different items, belonging to different categories and we can choose a category to hide or display. This is very convenient for users if they only want to view items belonging only to a particular category. However, list filtering can become one of the most resource-intensive workloads on a server.</p>
<p>Imagine a database table with the following data:</p>
<p><b>tbl_fruits</b></p>
<table width=300 cellspacing=0 cellpadding=2 border=1>
<tr style="background-color:#ccf">
<td><b>fruit</b></td>
<td><b>color</b></td>
</tr>
<tr>
<td>Apple</td>
<td>Red</td>
</tr>
<tr>
<td>Grape</td>
<td>Blue</td>
</tr>
<tr>
<td>Lemon</td>
<td>Yellow</td>
</tr>
<tr>
<td>Cherry</td>
<td>Red</td>
</tr>
<tr>
<td>Banana</td>
<td>Yellow</td>
</tr>
<tr>
<td>Strawberry</td>
<td>Red</td>
</tr>
<tr>
<td>Blueberry</td>
<td>Blue</td>
</tr>
<tr>
<td>Raspberry</td>
<td>Red</td>
</tr>
<tr>
<td>Pineapple</td>
<td>Yellow</td>
</tr>
</table>
<p><span id="more-751"></span><br />
The user can then choose to display all of the data, or filter the results based on their color.<br />
Traditionally, queries like this are handled through the backend. When a user selects a color, it would send a query to our database like this:</p>
<p><span class="code">SELECT fruit FROM `tbl_fruits` WHERE color = &#8220;red&#8221;;</span></p>
<p>In this example, the user selects the color red. This is the most common way of filtering a list. The problem lies however in that the more users requesting the data from our database, the more strain it gives on our server.</p>
<p>However, there is another solution: We can simply cache the most common database results so we don&#8217;t strain our database too much. This is what some programs like <a href="http://www.danga.com/memcached/" rel="nofollow" target="_blank">memcached</a> do.</p>
<p>Even with memory caching, it still doesn&#8217;t deal with the fact that clients will have to request with the server multiple times for each request in a category.</p>
<p>With smaller lists however, we can perform client-side list filtering. This is where jQuery comes in. With client-side list filtering, we only have to request the page once, and let the client&#8217;s browser do the work for us. In this way, most of the workload is distributed to the client computers.</p>
<p>So how do we make a list filter in jQuery?</p>
<p>Its quite simple really, and it is more of a matter of labeling our element classes correctly. Given our example database from above, we can simply write a back end script to output each item with the element&#8217;s class as its category in our database.</p>
<p>Given our database example, for this demo, this is how we would show our form:</p>
<pre><code class="html">Select Color Category:
&lt;select name="catSelect" id="catSelect"&gt;
	&lt;option value="all"&gt;Show All&lt;/option&gt;
	&lt;option value="red"&gt;Red&lt;/option&gt;
	&lt;option value="blue"&gt;Blue&lt;/option&gt;
	&lt;option value="yellow"&gt;Yellow&lt;/option&gt;
&lt;/select&gt;
&lt;input type="button" id="btnFilter" value="Filter the list"&gt;
&lt;ul&gt;
	&lt;li class="item red"&gt;Apple&lt;/li&gt;
	&lt;li class="item blue"&gt;Grape&lt;/li&gt;
	&lt;li class="item yellow"&gt;Lemon&lt;/li&gt;
	&lt;li class="item red"&gt;Cherry&lt;/li&gt;
	&lt;li class="item yellow"&gt;Banana&lt;/li&gt;
	&lt;li class="item red"&gt;Strawberry&lt;/li&gt;
	&lt;li class="item blue"&gt;Blueberry&lt;/li&gt;
	&lt;li class="item red"&gt;Raspberry&lt;/li&gt;
	&lt;li class="item yellow"&gt;Pineapple&lt;/li&gt;
&lt;/ul&gt;</code><code></code></pre>
<p>In this set-up notice that:</p>
<p><b><span class="code">#catSelect</span></b> &#8211; This is the id of the select box, we need this to get its value<br />
<b><span class="code">#btnFilter</span></b> &#8211; This is the id of the button that will trigger the filter event<br />
<b><span class="code">.item</span></b> &#8211; This is the class of all the items from the list. This is important so we can show all.<br />
<b><span class="code">.red, .blue, .yellow</span></b> &#8211; Depending on the fruit&#8217;s category, we will apply the appropriate class.</p>
<p>With all of these considerations in mind, this is how our final jQuery script will look like:</p>
<pre><code class="javascript">$(document).ready(function()
{
	$("#btnFilter").click(function()
	{
		var selection = $("#catSelect").val();

		if (selection == "all")
		{
			//show all items
			$(".item").show();
		}
		else
		{
			$(".item").hide();
			$("."+selection).show();
		}
	});
});</code></pre>
<p>What happens in this code is that whenever the <span class="code">#btnFilter</span> button is clicked, it triggers an event where we get the value of the category from select box <span class="code">#catSelect</span>.</p>
<p>If the &#8220;all&#8221; value is selected, we would show all of the list. However, if only a specific category is specified, we will hide all of the other categories and only show the items with the proper class.</p>
<p>A working demo of this article can be found below:<br />
<a href="http://jetlogs.org/jquery/jquery_list_filter.html" target="_blank">jQuery: List Filter Demo</a></p>
<p>The source code for the demo can be downloaded here:<br />
<a href="http://jetlogs.org/jquery/jquery_list_filter.zip">jQuery: List Filter Demo Source Code</a></p>
<p>The continuation of this article can be found below:<br />
<a href="http://jetlogs.org/2009/04/03/filtering-lists-with-multiple-categories-using-jquery/">Filtering Lists With Multiple Categories Using jQuery</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2009%2F02%2F10%2Ffiltering-lists-using-jquery%2F&amp;title=Filtering+Lists+Using+jQuery" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2009%2F02%2F10%2Ffiltering-lists-using-jquery%2F&amp;title=Filtering+Lists+Using+jQuery" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2009%2F02%2F10%2Ffiltering-lists-using-jquery%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2009%2F02%2F10%2Ffiltering-lists-using-jquery%2F&amp;title=Filtering+Lists+Using+jQuery" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2009%2F02%2F10%2Ffiltering-lists-using-jquery%2F&amp;title=Filtering+Lists+Using+jQuery" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2009%2F02%2F10%2Ffiltering-lists-using-jquery%2F&amp;title=Filtering+Lists+Using+jQuery" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2009%2F02%2F10%2Ffiltering-lists-using-jquery%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2009%2F02%2F10%2Ffiltering-lists-using-jquery%2F&amp;t=Filtering+Lists+Using+jQuery" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2009/02/10/filtering-lists-using-jquery/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Re-binding jQuery Events on AJAX Callbacks</title>
		<link>http://jetlogs.org/2009/01/29/re-binding-jquery-events-on-ajax-callbacks/</link>
		<comments>http://jetlogs.org/2009/01/29/re-binding-jquery-events-on-ajax-callbacks/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 05:48:14 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[unbind]]></category>

		<guid isPermaLink="false">http://jetlogs.org/?p=742</guid>
		<description><![CDATA[Or the title can be put shortly as: Why doesn&#8217;t my jQuery events work after an AJAX callback? Lets create a very simple example jQuery script which simply changes a list&#8217;s class on hover: We&#8217;ll assign red as the hover color: &#60;style type="text/css"&#62; .hover { color: #f00; } &#60;/style&#62; Then add the jQuery hover() event [...]]]></description>
			<content:encoded><![CDATA[<p>Or the title can be put shortly as: Why doesn&#8217;t my jQuery events work after an AJAX callback?</p>
<p>Lets create a very simple example jQuery script which simply changes a list&#8217;s class on hover:</p>
<p>We&#8217;ll assign red as the hover color:</p>
<pre><code class="html">&lt;style type="text/css"&gt;
.hover
{
	color: #f00;
}
&lt;/style&gt;</code></pre>
<p>Then add the jQuery <span class="code">hover()</span> event to toggle/change the list&#8217;s class when the mouse hovers over the list. This is how the javascript code will look like inside the <span class="code">document.ready()</span> function:</p>
<pre><code class="javascript">$("li").hover(
function()
{
	$(this).toggleClass("hover");
},
function()
{
	$(this).toggleClass("hover");
});</code></pre>
<p><span id="more-742"></span></p>
<p>To simplify things, I&#8217;ve added a sample AJAX callback function which is triggered by a button:</p>
<pre><code class="html">&lt;div id="container"&gt;
&lt;ul&gt;&lt;li&gt;This list should trigger the hover event&lt;/li&gt;&lt;/ul&gt;
The AJAX callback loads here...
&lt;/div&gt;

&lt;input type="button" id="btnLoad" value="Click me to change list"&gt;</code></pre>
<p>Our javascript would then look like this:</p>
<pre><code class="javascript">$(document).ready(function()
{
	$("li").hover(
	function()
	{
		$(this).toggleClass("hover");
	},
	function()
	{
		$(this).toggleClass("hover");
	});

	$("#btnLoad").click(function()
	{
		$.ajax(
		{
			url: "samplelist.html",
			success: function(html)
			{
				$("#container").empty().append(html);
			}
		});
	});

});</code></pre>
<p>The completed demo of the code above can be found here:<br />
<a href="http://jetlogs.org/jquery/jquery_binding_doesnt_work.html">jQuery: Non-working Callback Events</a></p>
<p>When you click the button and load the AJAX callback, notice that the <span class="code">hover()</span> event no longer triggers? This problem is also applicable to other jQuery events suck as <span class="code">click()</span>, <span class="code">blur()</span>, and others.</p>
<p>Lets take a look at what happens:</p>
<p>When we set the hover event for our lists, what is actually does is that it adds a javascript event and its matching function for each of the matched elements. So upon loading of the page, this is what happens to our list. It turns from:</p>
<pre><code class="html">&lt;li&gt;...&lt;/li&gt;</code></pre>
<p>to:</p>
<pre><code class="html">&lt;li onmouseenter="function()..." onmouseleave="..."&gt;...&lt;/li&gt;</code></pre>
<p>But what we forgot is that is that when we loaded the AJAX callback, it still doesn&#8217;t have the event binded to the element:</p>
<pre><code class="html">&lt;li&gt;AJAX callback loaded me&lt;/li&gt;</code></pre>
<p>So our hover event doesn&#8217;t trigger anymore because our AJAX callback doesn&#8217;t have one in the first place!</p>
<p>So how do we fix this? It quite simple. We just need to rebind the hover event to our AJAX callback. Running this function again:</p>
<pre><code class="javascript">$("li").hover(
function()
{
	$(this).toggleClass("hover");
},
function()
{
	$(this).toggleClass("hover");
});</code></pre>
<p>will make our hovers work again. But to make it even easier on our part, we can simply move all the initial bindings into a separate function and just call that on our <span class="code">document.ready()</span> and AJAX success functions.</p>
<p>Remember, we don&#8217;t have to move all the bindings, just the ones that are affected by the AJAX callback. In this example&#8217;s case, we only need the binding for the hover event on our list.</p>
<p>By doing this, our javascript will now look like this:</p>
<pre><code class="javascript">$(document).ready(function()
{
	initBinding();

	$("#btnLoad").click(function()
	{
		$.ajax(
		{
			url: "samplelist.html",
			success: function(html)
			{
				$("#container").empty().append(html);
				initBinding();
			}
		});
	});

});

function initBinding()
{
	$("li").hover(
	function()
	{
		$(this).toggleClass("hover");
	},
	function()
	{
		$(this).toggleClass("hover");
	});
}</code></pre>
<p>Now we no longer have to worry why our Javascript events and validations don&#8217;t work on AJAX callbacks.</p>
<p>The working demo of this page can be found here:<br />
<a href="http://jetlogs.org/jquery/jquery_binding_works.html">jQuery: Re-binding jQuery Events on AJAX Callbacks</a></p>
<p>Or if you want to download all the source codes, you can follow the link below:<br />
<a href="http://jetlogs.org/jquery/jquery_binding.zip">Download jQuery: Re-binding jQuery Events on AJAX Callbacks Source</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2009%2F01%2F29%2Fre-binding-jquery-events-on-ajax-callbacks%2F&amp;title=Re-binding+jQuery+Events+on+AJAX+Callbacks" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2009%2F01%2F29%2Fre-binding-jquery-events-on-ajax-callbacks%2F&amp;title=Re-binding+jQuery+Events+on+AJAX+Callbacks" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2009%2F01%2F29%2Fre-binding-jquery-events-on-ajax-callbacks%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2009%2F01%2F29%2Fre-binding-jquery-events-on-ajax-callbacks%2F&amp;title=Re-binding+jQuery+Events+on+AJAX+Callbacks" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2009%2F01%2F29%2Fre-binding-jquery-events-on-ajax-callbacks%2F&amp;title=Re-binding+jQuery+Events+on+AJAX+Callbacks" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2009%2F01%2F29%2Fre-binding-jquery-events-on-ajax-callbacks%2F&amp;title=Re-binding+jQuery+Events+on+AJAX+Callbacks" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2009%2F01%2F29%2Fre-binding-jquery-events-on-ajax-callbacks%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2009%2F01%2F29%2Fre-binding-jquery-events-on-ajax-callbacks%2F&amp;t=Re-binding+jQuery+Events+on+AJAX+Callbacks" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2009/01/29/re-binding-jquery-events-on-ajax-callbacks/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>jQuery: Multiple Checkbox Validation</title>
		<link>http://jetlogs.org/2008/08/19/jquery-multiple-checkbox-validation/</link>
		<comments>http://jetlogs.org/2008/08/19/jquery-multiple-checkbox-validation/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 03:52:22 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://jetlogs.org/?p=525</guid>
		<description><![CDATA[Having problems with checkbox validations? Then you&#8217;re not alone. Checkbox validation is usually more difficult due to its nature of having multiple values at the same time. Consider this simple form where a user is required to select at least one checkbox: Which of the following jQuery selectors do you usually use? (Please select at [...]]]></description>
			<content:encoded><![CDATA[<p>Having problems with checkbox validations? Then you&#8217;re not alone. Checkbox validation is usually more difficult due to its nature of having multiple values at the same time.</p>
<p>Consider this simple form where a user is required to select at least one checkbox:<br />
<b>Which of the following jQuery selectors do you usually use?<br />
(Please select at least one)</b></p>
<input type="checkbox" name="selector[]" id="selector" value="id"/>#id</p>
<input type="checkbox" name="selector[]" id="selector" value="class"/>.class</p>
<input type="checkbox" name="selector[]" id="selector" value="element"/>element[@attribute]</p>
<p><b>Which of the following jQuery events do you usually encounter?<br />
(Please select at least one)</b></p>
<input type="checkbox" name="event[]" id="event" value="click"/>click</p>
<input type="checkbox" name="event[]" id="event" value="focus"/>focus</p>
<input type="checkbox" name="event[]" id="event" value="change"/>change</p>
<input type="checkbox" name="event[]" id="event" value="blur"/>blur</p>
<input type="checkbox" name="event[]" id="event" value="submit"/>submit</p>
<pre><code class="html">&lt;b&gt;Which of the following jQuery selectors
do you usually use? (Please select at least one)&lt;/b&gt;&lt;br&gt;
&lt;input type="checkbox" name="selector[]"
id="selector" value="id"&gt;#id&lt;br&gt;
&lt;input type="checkbox" name="selector[]"
id="selector" value="class"&gt;.class&lt;br&gt;
&lt;input type="checkbox" name="selector[]"
id="selector" value="element"&gt;element[@attribute]&lt;br&gt;&lt;br&gt;

&lt;b&gt;Which of the following jQuery events
do you usually encounter? (Please select at least one)&lt;/b&gt;&lt;br&gt;
&lt;input type="checkbox" name="event[]"
id="event" value="click"&gt;click&lt;br&gt;
&lt;input type="checkbox" name="event[]"
id="event" value="focus"&gt;focus&lt;br&gt;
&lt;input type="checkbox" name="event[]"
id="event" value="change"&gt;change&lt;br&gt;
&lt;input type="checkbox" name="event[]"
id="event" value="blur"&gt;blur&lt;br&gt;
&lt;input type="checkbox" name="event[]"
id="event" value="submit"&gt;submit&lt;br&gt;</code></pre>
<p>So how do we do the validation?</p>
<p>First we&#8217;ll have to bind the validations with the form&#8217;s submit event (<span class="code">submit()</span> event on jQuery) and return false for all empty checkbox groups to prevent the form triggering its submission.</p>
<p>Without using jQuery, you&#8217;ll have to iterate through all of the checkbox elements, then check for the checked status of each individual check boxes (if none is checked then return false). And you&#8217;ll have to do this for each check box grouping.</p>
<p>However, jQuery makes things a little bit easier by introducing a selector filter for checked elements.<br />
Using the <b>:checked</b> filter, it will select all of the checked elements for a particular element. so in this case,</p>
<pre><code class="javascript">$("input:checked")</code></pre>
<p>will select all of the checked input elements. However, this is a downside for this. The <b>:checked</b> filter does not support <span class="code">#id</span> and <span class="code">.class</span> selectors so the following won&#8217;t work:</p>
<pre><code class="javascript">$("#selector:checked")
$("#event:checked")</code></pre>
<p>So how do we work around this? Its pretty simple, by specifying the id, class, or even as an attribute, we can now separate the checkboxes according to its grouping. In this example, we are going to be using the <span class="code">#id</span> to separate our checkboxes.</p>
<pre><code class="javascript">var selector_checked = $("input[@id=selector]:checked").length;
var event_checked = $("input[@id=event]:checked").length;
if (selector_checked == 0)
{
	return false;
}
else if (selector_checked == 0)
{
	return false;
}
else
{
	return true;
}</code></pre>
<p>Its much simpler than iterating through each checkbox, since jQuery automatically does this for us. For two different checkbox groups, this is acceptable. However, when we would need to deal with more checkbox groupings, this can become a problem since the code will become more cluttered by then.</p>
<p>To simplify things, we&#8217;ll just make our own function passing the <span class="code">#id</span> as a parameter to check for each checkbox group&#8217;s status. This is how the script would finally look like.</p>
<pre><code class="javascript">$(document).ready(function()
{
	$("form").submit(function()
	{
		if (!isCheckedById("selector"))
		{
			alert ("Please select at least one selector");
			return false;
		}
		else if (!isCheckedById("event"))
		{
			alert ("Please select at least one event");
			return false;
		}
		else
		{
			return true; //submit the form
		}
	});

	function isCheckedById(id)
	{
		var checked = $("input[@id="+id+"]:checked").length;
		if (checked == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
});</code></pre>
<ul>
<li><a href="http://jetlogs.org/jquery/checkbox_validation.html">View Demo</a></li>
<li><a href="http://jetlogs.org/jquery/checkbox_validation.zip">Download Source</a></li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2008%2F08%2F19%2Fjquery-multiple-checkbox-validation%2F&amp;title=jQuery%3A+Multiple+Checkbox+Validation" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2008%2F08%2F19%2Fjquery-multiple-checkbox-validation%2F&amp;title=jQuery%3A+Multiple+Checkbox+Validation" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2008%2F08%2F19%2Fjquery-multiple-checkbox-validation%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2008%2F08%2F19%2Fjquery-multiple-checkbox-validation%2F&amp;title=jQuery%3A+Multiple+Checkbox+Validation" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2008%2F08%2F19%2Fjquery-multiple-checkbox-validation%2F&amp;title=jQuery%3A+Multiple+Checkbox+Validation" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2008%2F08%2F19%2Fjquery-multiple-checkbox-validation%2F&amp;title=jQuery%3A+Multiple+Checkbox+Validation" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2008%2F08%2F19%2Fjquery-multiple-checkbox-validation%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2008%2F08%2F19%2Fjquery-multiple-checkbox-validation%2F&amp;t=jQuery%3A+Multiple+Checkbox+Validation" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2008/08/19/jquery-multiple-checkbox-validation/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>jqModal: Draggable Floating Dialog</title>
		<link>http://jetlogs.org/2008/06/04/jqmodal-draggable-floating-dialog/</link>
		<comments>http://jetlogs.org/2008/06/04/jqmodal-draggable-floating-dialog/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 08:24:09 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[dialog]]></category>
		<category><![CDATA[floating]]></category>
		<category><![CDATA[jqmodal]]></category>

		<guid isPermaLink="false">http://jetlogs.org/?p=459</guid>
		<description><![CDATA[Here is an alternate tutorial on how to create a floating dialog using the jQuery library with the jqModal plugin. Aside from providing an easy way to create you floating dialog, the jqModal extension also allows you to drag and resize your dialog. For this tutorial, I will demonstrate how to create a draggable floating [...]]]></description>
			<content:encoded><![CDATA[<p>Here is an alternate tutorial on how to create a floating dialog using the jQuery library with the <a href="http://dev.iceburg.net/jquery/jqModal/" target="_blank">jqModal plugin</a>. Aside from providing an easy way to create you floating dialog, the jqModal extension also allows you to drag and resize your dialog. For this tutorial, I will demonstrate how to create a draggable floating dialog.</p>
<p>First of all, in order to use jqModal, the jQuery library is a prerequisite. And since we will add the drag functionality for our dialog, we will also need to include jqModal&#8217;s Drag and Resize plugin. Aside from jqModal&#8217;s script library, it also comes with a default CSS which will take care of the default styles. To customize this default CSS, we can simply override it later on. But for now, let&#8217;s include the required scripts and styles:</p>
<pre><code class='html'>&lt;link href="jqModal.css" rel="stylesheet" type="text/css" /&gt;
&lt;script type="text/javascript" src="jquery-1.2.6.pack.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="jqModal.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="jqDnR.js"&gt;&lt;/script&gt;</code></pre>
<p>Now the next step is creating the layer for the floating dialog itself. Before we go on any further, lets take a look at jqModal&#8217;s predefined special classes. By assigning these special classes to elements, they will automatically be configured depending on its class. You can either use the predefined classes, or you can <a href="http://dev.iceburg.net/jquery/jqModal/README" target="_blank">create your own triggers</a>.  But for now, we&#8217;ll just use the predefined classes:</p>
<p><span class="code">.jqmWindow</span> &#8211; defines the styles for the dialog window (visibility, z-index)<br />
<span class="code">.jqModal</span> &#8211; defines a trigger for displaying the dialog window<br />
<span class="code">.jqmClose</span> &#8211; defines a trigger for closing the dialog window</p>
<p>For this demo we will only use the <span class="code">.jqmWindow</span> and <span class="code">.jqmClose</span> classes since we will add a custom event to our jqmWindow. </p>
<p>For this demo, we would also need some ids to identify our floating layer and its handle. We will use <span class="code">#previewLayer</span> for the floating dialog and <span class="code">#handle</span> for its handle. For the dialog&#8217;s content, we&#8217;ll use <span class="code">#previewContent</span>. Here is how it would look like:</p>
<pre><code class='html'>&lt;div id="previewLayer" class="jqmWindow"&gt;
	&lt;div id="handle"&gt;
		&lt;a href="#" class="jqmClose"&gt;close window&lt;/a&gt;
	&lt;/div&gt;
	&lt;div id="previewContent"&gt;&lt;/div&gt;
&lt;/div&gt;</code></pre>
<p>Now that we have our dialog and our trigger, let&#8217;s set them up at out $.(document).ready() function:</p>
<pre><code class="javascript">$(document).ready(function()
{
	$('#previewLayer').jqm().jqDrag('#handle');
}</code></pre>
<p>The following line of code simple means to make <span class="code">#previewLayer</span> a modal window with <span class="code">#handle</span> as its handle.</p>
<p>Now that we have created our own floating dialog, now its time to add some back-end logic for our window. For demonstration purposes, we&#8217;ll use it as a preview for a sample form:</p>
<pre><code class="html">First Name: &lt;input type="text" id="fname" name="fname" /&gt;&lt;br /&gt;
Last Name: &lt;input type="text" id="lname" name="lname" /&gt;&lt;br /&gt;&lt;br /&gt;
Gender:&lt;br/&gt;
&lt;input type="radio" name="gender" id="gender" value="Male"/&gt;
 Male
&lt;input type="radio" name="gender" id="gender" value="Female"/&gt;
 Female&lt;br /&gt;&lt;br /&gt;

&lt;input type="button" id="previewButton" value="Preview" /&gt;</code></pre>
<p>What we&#8217;ll simply do is replace the contents of <span class="code">#previewContent</span> with the values inside the form when the <span class="code">#previewButton</span> is triggered:</p>
<pre><code class="javascript">function generatePreview()
{
	var fname = $('#fname').val();
	var lname = $('#lname').val();
	var gender = $('input[@id=gender][@checked]').val();

	if (!gender)
	{
		gender = '';
	}

	var preview = '&lt;h2&gt;Preview&lt;/h2&gt;&lt;hr /&gt;';
	preview += '&lt;b&gt;First Name:&lt;/b&gt; ' + fname + '&lt;br /&gt;';
	preview += '&lt;b&gt;Last Name:&lt;/b&gt; ' + lname + '&lt;br /&gt;';
	preview += '&lt;b&gt;Gender:&lt;/b&gt; ' + gender + '&lt;br /&gt;';

	return preview;
}

$(document).ready(function()
{
	$('#previewLayer').jqm().jqDrag('#handle');

	$('#previewButton').click(function()
	{
		var content = generatePreview();

		$('#previewContent').empty().append(content);
		$('#previewLayer').jqmShow();
	});
});</code></pre>
<p>If you want to see the demo in action, just click below<br />
<a href="http://jetlogs.org/jquery/jquery_jqmodal_floating_dialog.html">View Demo</a> | <a href="http://jetlogs.org/jquery/jquery_floating_dialog2.zip">Download Source</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2008%2F06%2F04%2Fjqmodal-draggable-floating-dialog%2F&amp;title=jqModal%3A+Draggable+Floating+Dialog" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2008%2F06%2F04%2Fjqmodal-draggable-floating-dialog%2F&amp;title=jqModal%3A+Draggable+Floating+Dialog" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2008%2F06%2F04%2Fjqmodal-draggable-floating-dialog%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2008%2F06%2F04%2Fjqmodal-draggable-floating-dialog%2F&amp;title=jqModal%3A+Draggable+Floating+Dialog" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2008%2F06%2F04%2Fjqmodal-draggable-floating-dialog%2F&amp;title=jqModal%3A+Draggable+Floating+Dialog" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2008%2F06%2F04%2Fjqmodal-draggable-floating-dialog%2F&amp;title=jqModal%3A+Draggable+Floating+Dialog" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2008%2F06%2F04%2Fjqmodal-draggable-floating-dialog%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2008%2F06%2F04%2Fjqmodal-draggable-floating-dialog%2F&amp;t=jqModal%3A+Draggable+Floating+Dialog" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2008/06/04/jqmodal-draggable-floating-dialog/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>jQuery: How to Fix the IE Select Box z-index Bug</title>
		<link>http://jetlogs.org/2008/05/09/jquery-fix-ie-select-box-z-index-bug/</link>
		<comments>http://jetlogs.org/2008/05/09/jquery-fix-ie-select-box-z-index-bug/#comments</comments>
		<pubDate>Fri, 09 May 2008 04:01:20 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[iframe]]></category>
		<category><![CDATA[select box]]></category>

		<guid isPermaLink="false">http://jetlogs.org/?p=447</guid>
		<description><![CDATA[Had you ever experienced the IE6 z-index bug where a simple select box will always take priority over any predefined z-indices and always show up on top? Ever annoyed to make a floating dialog only to have a select box underneath popup and ruin the dialog? Well you&#8217;re not alone&#8230; I&#8217;m in your divs, ruining [...]]]></description>
			<content:encoded><![CDATA[<p>Had you ever experienced the IE6 z-index bug where a simple select box will always take priority over any predefined z-indices and always show up on top? Ever annoyed to make a <a href="http://jetlogs.org/jquery/floating_dialog.php">floating dialog</a> only to have a select box underneath popup and ruin the dialog? Well you&#8217;re not alone&#8230;</p>
<p><img src="http://jetlogs.org/wp-content/uploads/2008/05/selectbox_bug.gif" alt="Example of the Select Box Bug" title="selectbox_bug" width="264" height="182"/><br />
<strong>I&#8217;m in your divs, ruining your z-index</strong><span id="more-447"></span></p>
<p>Fortunately, there is a fix for this bug. By using an iframe and expanding it over the select box area, the iframe will take precendence over the select box. However, implementing this iframe hack alone can be quite a bit of a hassle.</p>
<p>Thankfully, there is alread a jQuery plugin that will automatically create the iframe for you. The plugin is simply called <a href="http://plugins.jquery.com/project/bgiframe">bgiframe</a> and using it is  quick and easy. Just include the script and attach the bgiframe() method to all of your floating layers. Here is a short and quick example:</p>
<pre><code class="html">&lt;head&gt;
	&lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;
	&lt;script type="text/javascript" src="jquery.bgiframe.js"&gt;&lt;/script&gt;
	&lt;script type="text/javascript"&gt;
		$(document).ready(function()
		{
			$('#floating_div').bgiframe();
		}
	&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;select&gt;
		&lt;option&gt;Test&lt;/option&gt;
	&lt;/select&gt;
	&lt;div id="floating_div"&gt;I'm over this select box&lt;/div&gt;
&lt;/body&gt;</code></pre>
<p>Now those nasty z-index bugs won&#8217;t ever trouble you ever again.</p>
<p>Download:<br />
<a href="http://plugins.jquery.com/project/bgiframe">bgiframe Plugin Page</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2008%2F05%2F09%2Fjquery-fix-ie-select-box-z-index-bug%2F&amp;title=jQuery%3A+How+to+Fix+the+IE+Select+Box+z-index+Bug" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2008%2F05%2F09%2Fjquery-fix-ie-select-box-z-index-bug%2F&amp;title=jQuery%3A+How+to+Fix+the+IE+Select+Box+z-index+Bug" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2008%2F05%2F09%2Fjquery-fix-ie-select-box-z-index-bug%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2008%2F05%2F09%2Fjquery-fix-ie-select-box-z-index-bug%2F&amp;title=jQuery%3A+How+to+Fix+the+IE+Select+Box+z-index+Bug" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2008%2F05%2F09%2Fjquery-fix-ie-select-box-z-index-bug%2F&amp;title=jQuery%3A+How+to+Fix+the+IE+Select+Box+z-index+Bug" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2008%2F05%2F09%2Fjquery-fix-ie-select-box-z-index-bug%2F&amp;title=jQuery%3A+How+to+Fix+the+IE+Select+Box+z-index+Bug" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2008%2F05%2F09%2Fjquery-fix-ie-select-box-z-index-bug%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2008%2F05%2F09%2Fjquery-fix-ie-select-box-z-index-bug%2F&amp;t=jQuery%3A+How+to+Fix+the+IE+Select+Box+z-index+Bug" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2008/05/09/jquery-fix-ie-select-box-z-index-bug/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>jQuery: AJAX Tabs</title>
		<link>http://jetlogs.org/2008/03/17/jquery-ajax-tabs/</link>
		<comments>http://jetlogs.org/2008/03/17/jquery-ajax-tabs/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 08:11:08 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[preloader]]></category>
		<category><![CDATA[tabs]]></category>

		<guid isPermaLink="false">http://jetlogs.org/2008/03/17/jquery-ajax-tabs/</guid>
		<description><![CDATA[One of AJAX&#8217;s strengths is that it allows you to save bandwidth for your application by providing data on-demand instead of loading everything in one go. Because of this, some applications like the invisionboard has employed AJAX tabs on its user control panel. But have you ever wondered how to create one yourself? Well this [...]]]></description>
			<content:encoded><![CDATA[<p>One of AJAX&#8217;s strengths is that it allows you to save bandwidth for your application by providing data on-demand instead of loading everything in one go. Because of this, some applications like the invisionboard has employed AJAX tabs on its user control panel. But have you ever wondered how to create one yourself? Well this article will show you how its done easily using jQuery.<span id="more-410"></span></p>
<p>First of all, the concept of AJAX Tabs is very simple if you would look at it from the ground up. By concept, it is simply multiple AJAX triggers that have the same target container. For this example, we will use a link as the trigger and have a div with an id of <span class="code">tabcontents</span> as the target container.</p>
<pre><code class="html">&lt;a id="tab1" href="#"&gt;Tab 1&lt;/a&gt;&lt;br&gt;
&lt;div id="tabcontent"&gt;&lt;/div&gt;
</code></pre>
<p>First let&#8217;s do a sample of a simple AJAX trigger from a link with an id of <span class="code">tab1</span> loading an HTML page named <span class="code">tab1.html</span> through AJAX. Then the page will be loaded to a div with an id of <span class="code">tabcontent</span>:</p>
<pre><code class="javascript">$("#tab1").click(function()
{
	$.ajax(
	{
		url: "tab1.html",
		cache: false,
		success: function(message)
		{
			$("#tabcontent").empty().append(message);
		}
	});
});</code></pre>
<p>Its quite simple if you would look at it. The important thing is we have to match the trigger to the page we have to load, and into the container in which we must load the page.</p>
<p>If for example, we want to create a tab with 4 tabs / triggers we can simply manage this through repeating the code above several times and just changing the trigger id&#8217;s and the page it loads. The target container will still remain the same. However, this will present a problem since the more tabs / triggers we have, the more the clutter, and the more it is harder to manage. </p>
<p>Fortunately, there is a solution to this. By turning the AJAX call into a function and passing the page required to load as a parameter, we can simplify the tab implementation:</p>
<pre><code class="javascript">function loadTab(pageUrl)
{
	$.ajax(
	{
		url: pageUrl,
		cache: false,
		success: function(message)
		{
			$("#tabcontent").empty().append(message);
		}
	});
}

$(document).ready(function()
{
	$("#tab1").click(function()
	{
		loadTab("tab1.html");
	});

	$("#tab2").click(function()
	{
		loadTab("tab2.html");
	});

	$("#tab3").click(function()
	{
		loadTab("tab3.html");
	});

	$("#tab4").click(function()
	{
		loadTab("tab4.html");
	});
});</code></pre>
<p>Want to make it even more manageable? Why not use arrays to store the required pages, and pass the array keys instead? And while we&#8217;re at it, we can also add a <a href="http://jetlogs.org/jquery/jquery_preloader_using_form.php">preloader</a> for when the page is still loading. Here is what the final code will look like:</p>
<pre><code class="javascript">var pageUrl = new Array();
pageUrl[1] = "tab1.html";
pageUrl[2] = "tab2.html";
pageUrl[3] = "tab3.html";
pageUrl[4] = "tab4.html";		

function loadTab(id)
{
	if (pageUrl[id].length > 0)
	{
		$("#preloader").show();
		$.ajax(
		{
			url: pageUrl[id],
			cache: false,
			success: function(message)
			{
			    $("#tabcontent").empty().append(message);
			    $("#preloader").hide();
			}
		});
	}
}

$(document).ready(function()
{
	$("#preloader").hide();

	$("#tab1").click(function()
	{
		loadTab(1);
	});

	$("#tab2").click(function()
	{
		loadTab(2);
	});

	$("#tab3").click(function()
	{
		loadTab(3);
	});

	$("#tab4").click(function()
	{
		loadTab(4);
	});
});</code></pre>
<p>This is merely a bare bones implementation of the AJAX tabs. From here, you can add additional validations (for example AJAX error messages), and add styles to your links and content. You can also specify dynamic content (like PHP, Python, or ASP scripts) as the target for the script to load.</p>
<p>If you want to see a demo of this script in action, or you want to download the source code, just click on the links below:</p>
<p><a href="http://jetlogs.org/jquery/jquery_ajax_tabs.html">jQuery AJAX Tabs Demo</a><br />
<a href="http://jetlogs.org/jquery/jquery_ajax_tabs.zip">Download jQuery AJAX Tabs Source Code</a> </p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2008%2F03%2F17%2Fjquery-ajax-tabs%2F&amp;title=jQuery%3A+AJAX+Tabs" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2008%2F03%2F17%2Fjquery-ajax-tabs%2F&amp;title=jQuery%3A+AJAX+Tabs" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2008%2F03%2F17%2Fjquery-ajax-tabs%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2008%2F03%2F17%2Fjquery-ajax-tabs%2F&amp;title=jQuery%3A+AJAX+Tabs" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2008%2F03%2F17%2Fjquery-ajax-tabs%2F&amp;title=jQuery%3A+AJAX+Tabs" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2008%2F03%2F17%2Fjquery-ajax-tabs%2F&amp;title=jQuery%3A+AJAX+Tabs" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2008%2F03%2F17%2Fjquery-ajax-tabs%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2008%2F03%2F17%2Fjquery-ajax-tabs%2F&amp;t=jQuery%3A+AJAX+Tabs" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2008/03/17/jquery-ajax-tabs/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>jQuery: Disabled and ReadOnly Inputs</title>
		<link>http://jetlogs.org/2007/12/16/jquery-disabled-and-readonly-inputs/</link>
		<comments>http://jetlogs.org/2007/12/16/jquery-disabled-and-readonly-inputs/#comments</comments>
		<pubDate>Sat, 15 Dec 2007 16:22:14 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[disabled]]></category>
		<category><![CDATA[enabled]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[readonly]]></category>
		<category><![CDATA[toggle]]></category>

		<guid isPermaLink="false">http://jetlogs.org/2007/12/16/jquery-disabled-and-readonly-inputs/</guid>
		<description><![CDATA[Ever wonder how to disable another input element through the use of another control? Here is a simple tutorial on how to toggle the disabled attribute of an input element. For the first demo, we will toggle an input&#8217;s disabled status through the use of another control. For this example, we will give our target [...]]]></description>
			<content:encoded><![CDATA[<p><script language="javascript" src="/jquery/disabled_input.js">></script>Ever wonder how to disable another input element through the use of another control? Here is a simple tutorial on how to toggle the disabled attribute of an input element.</p>
<p>For the first demo, we will toggle an input&#8217;s <span class="code">disabled</span> status through the use of another control. For this example, we will give our target input an id of <span class="code">target</span>, while our control, in this case a link, we will give it an id of <span class="code">control</span>:</p>
<pre><code class="html">Target text field: &lt;input type="text" id="target" /&gt;&lt;br /&gt;
&lt;a href="#" id="control" /&gt;Disable target&lt;/a&gt;</code></pre>
<p>Now to toggle the <span class="code">target</span>&#8216;s disabled status, it is actually quite simple in jQuery. Using the <span class="code">.attr()</span> attribute we can disable the <span class="code">target</span>. Then using the <span class="code">.removeAttr()</span>, we can remove the disabled status. Here is how the javascript code would look like:</p>
<pre><code class="javascript">$("#control").toggle(
function ()
{
	$('#target').attr("disabled", true);
},
function ()
{
	$('#target').removeAttr("disabled");
});</code></pre>
<p>Here is a demo on how to toggle the disabled status of an input:</p>
<p>Target text field:<br />
<input type="text" id="target" />
<a href="#" id="control" />Disable target</a></p>
<p>Although the first method is handy, there are times when we really don&#8217;t need a control input. What if we want to enable an input textbox just by clicking on it? Unfortunately<span id="more-330"></span>, when an input element is disabled, it would no longer register events such as <span class="code">focus</span> or <span class="code">click</span>. The good news is that there is an alternative. Instead of using the <span class="code">disabled</span> attribute, we use the <span class="code">readonly</span> attribute instead. Here is a sample javascript code on making a readonly input enabled using its click event:</p>
<pre><code class="javascript">$('#readonly').click(
function()
{
	if ($('#readonly').attr("readonly") == true)
	{
		$('#readonly').val('');
		$('#readonly').removeAttr("readonly");
	}
});</code></pre>
<p>Here is the demo of how to change the readonly attribute</p>
<p>This text field will no longer be read-only upon clicking:<br />
<input type="text" id="readonly" readonly value="click me to enable"/>
<p>If you want to download the source code for this tutorial,<br />
just click on the link below:</p>
<p><a href="/jquery/jquery_disabled_input.zip" title="jQuery: Disabled and ReadOnly Inputs source code">Download Source Code</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F16%2Fjquery-disabled-and-readonly-inputs%2F&amp;title=jQuery%3A+Disabled+and+ReadOnly+Inputs" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F16%2Fjquery-disabled-and-readonly-inputs%2F&amp;title=jQuery%3A+Disabled+and+ReadOnly+Inputs" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F16%2Fjquery-disabled-and-readonly-inputs%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F16%2Fjquery-disabled-and-readonly-inputs%2F&amp;title=jQuery%3A+Disabled+and+ReadOnly+Inputs" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F16%2Fjquery-disabled-and-readonly-inputs%2F&amp;title=jQuery%3A+Disabled+and+ReadOnly+Inputs" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F16%2Fjquery-disabled-and-readonly-inputs%2F&amp;title=jQuery%3A+Disabled+and+ReadOnly+Inputs" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F16%2Fjquery-disabled-and-readonly-inputs%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F16%2Fjquery-disabled-and-readonly-inputs%2F&amp;t=jQuery%3A+Disabled+and+ReadOnly+Inputs" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2007/12/16/jquery-disabled-and-readonly-inputs/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Javascript Keypress Validations</title>
		<link>http://jetlogs.org/2007/12/09/javascript-keypress-validations/</link>
		<comments>http://jetlogs.org/2007/12/09/javascript-keypress-validations/#comments</comments>
		<pubDate>Sun, 09 Dec 2007 02:48:49 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[onkeypress]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://jetlogs.org/2007/12/09/javascript-keypress-validations/</guid>
		<description><![CDATA[Most client-side validation nowadays is usually made through onsubmit validations. Although it is pretty convenient, it may be quite annoying for a user to find only at the end that most of their inputs are invalid and must enter them once again. If onblur validations is not enough, keyperss validation is the next step. Here [...]]]></description>
			<content:encoded><![CDATA[<p><script language="javascript" src="/js/keypress_validation.js"></script>Most client-side validation nowadays is usually made through <span class='code'>onsubmit</span> validations. Although it is pretty convenient, it may be quite annoying for a user to find only at the end that most of their inputs are invalid and must enter them once again. If <a href="/2007/09/14/jquery-textbox-validation-and-the-blur-event/">onblur validations</a> is not enough, keyperss validation is the next step. </p>
<p>Here is another alternative to the usual onsubmit validations for forms. Instead of validating a form only at the end, why not validate it as the user is typing their input. This is where keypress validations come in. We can capture it through the <span class='code'>onkeypress</span> event.</p>
<p>Here are some examples of keypress validations:</p>
<p>Only integers are allowed:<br />
<input type='text' onkeypress='return numOnly(event)'/><br/><br />
Only alphanumeric characters are allowed:<br />
<input type='text' onkeypress='return alphaNumOnly(event)'/><br/><br />
No spaces are allowed:<br />
<input type='text' onkeypress='return noWhiteSpace(event)'/><br/></p>
<p>Here is an example keypress validation function:<span id="more-328"></span></p>
<pre><code class='javascript'>function alphaOnly(evt)
{
	var charCode = (evt.which) ? evt.which : window.event.keyCode;

	if (charCode <= 13)
    {
    	return true;
    }
    else
    {
		var keyChar = String.fromCharCode(charCode);
		var re = /[a-zA-Z]/
		return re.test(keyChar);
	}
}</code></pre>
<p>What this function does is</p>
<ol>
<li>capture the keypress character entered</li>
<li>determine if the browser is navigator based or not by checking for the <span class='code'>which</span> or <span class='code'>keyCode</span> attribute</li>
<li>most of the special character codes will be skipped</li>
<li>validate the character entered with a regex pattern</li>
</ol>
<p>To use a keypress validation for an input element, we must add its onkeypress attribute with the validation function we want:</p>
<pre><code class='html'>&lt;input type='text' onkeypress='return alphaOnly(event)' /&gt;</code></pre>
<p>You can also download the Javascript source below:<br />
<a href="/downloads/keypress_validation.zip">Download javascript keypress validation script</a></p>
<p>PS: As I have always said before, client-side validation is no substitute for server-side validation. Client-size validation is more for usability and reducing  some load from your server (through reduced rendering of error pages). So don't be lazy!</p>
<p>UPDATE:<br />
For those who are interested in the jQuery version, here is the sample code:</p>
<pre><code class='javascript'>$("input").bind("keypress", function(evt)
{
    var charCode = (evt.which) ? evt.which : window.event.keyCode; 

    if (charCode <= 13)
    {
        return true;
    }
    else
    {
        var keyChar = String.fromCharCode(charCode);
        var re = /[a-zA-Z]/
        return re.test(keyChar);
    }
}</code></pre>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F09%2Fjavascript-keypress-validations%2F&amp;title=Javascript+Keypress+Validations" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F09%2Fjavascript-keypress-validations%2F&amp;title=Javascript+Keypress+Validations" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F09%2Fjavascript-keypress-validations%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F09%2Fjavascript-keypress-validations%2F&amp;title=Javascript+Keypress+Validations" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F09%2Fjavascript-keypress-validations%2F&amp;title=Javascript+Keypress+Validations" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F09%2Fjavascript-keypress-validations%2F&amp;title=Javascript+Keypress+Validations" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F09%2Fjavascript-keypress-validations%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2007%2F12%2F09%2Fjavascript-keypress-validations%2F&amp;t=Javascript+Keypress+Validations" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2007/12/09/javascript-keypress-validations/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Auto-saving with jQuery</title>
		<link>http://jetlogs.org/2007/11/11/auto-saving-with-jquery/</link>
		<comments>http://jetlogs.org/2007/11/11/auto-saving-with-jquery/#comments</comments>
		<pubDate>Sat, 10 Nov 2007 18:22:28 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://jetlogs.org/2007/11/11/auto-saving-with-jquery/</guid>
		<description><![CDATA[Auto-saving has been one of the great features that AJAX has given to us. Ever had a time when you were writing a very long article and suddenly, your browser crashes. Thanks to applications that implement auto-saving, data is less prone from lost due to these cases. Ever wondered how auto-saving is done? Basically, it [...]]]></description>
			<content:encoded><![CDATA[<p>Auto-saving has been one of the great features that AJAX has given to us. Ever had a time when you were writing a very long article and suddenly, your browser crashes. Thanks to applications that implement auto-saving, data is less prone from lost due to these cases. </p>
<p>Ever wondered how auto-saving is done? Basically, it is just a simple AJAX script running in the background at set intervals to save the data you are currently working on. Here is a simple tutorial on how to implement auto-saving using jQuery<span id="more-299"></span></p>
<p>Before we start, the article assumed the following:</p>
<ul>
<li>Basic knowledge of jQuery</li>
<li>Basic knowledge of PHP</li>
</ul>
<p>For the articles database, we will only use 4 rows for our table named <span class='code'>articles</span>:</p>
<ul>
<li><b>id</b> &#8211; this will serve as the primary index for the table; auto-increment</li>
<li><b>title</b> &#8211; this will contain the title of the article</li>
<li><b>content</b> &#8211; this will contain the body of the article itself</li>
<li><b>timestamp</b> &#8211; this will contain the timestamp when the row was last inserted or updated</li>
</ul>
<p>Let us first start with the form and call it <span class='code'>add_article.php</span> for this tutorial. Before we render our form, we must first create a blank instance of a row. The purpose of this is two-fold. One, so that we can generate an article id, and secondly that we cannot really update a non-existent row. There is a more efficient way of implementing this logic without generating a blank row every time, however, for the sake of simplicity and explaining the point of concept for this tutorial, we will skip this part.</p>
<p><b>add_article.php</b>
<pre><code class='php'>//create new article on database
mysql_query("INSERT INTO `articles`(title, content) VALUES('', '')");
$article_id = mysql_insert_id();</code></pre>
<p>The next step is making the form. First, we would create a form element that would to save the article to a standard save page. Lets call it <span class='code'>save.php</span>.</p>
<p>Remember the <span class='code'>article_id</span> generated in making the blank row? Now we must output it as a hidden input for the <span class='code'>article_id</span> associated with the article. We would also need an input text field for the <span class='code'>title</span> and a text area for the <span class='code'>content</span>. Finally we would need a submit button, and a placeholder container for the &#8220;Last saved&#8221; timestamp.</p>
<p><b>add_article.php</b>
<pre><code class='html'>&lt;form id="article_form" method="post" action="save.php"&gt;

Title:&lt;br /&gt;
&lt;input type="text" name="title" id="txt_title" /&gt;&lt;br /&gt;
Content:&lt;br /&gt;
&lt;textarea name="content" id="txt_content" &gt;&lt;/textarea&gt;&lt;br /&gt;
&lt;input type="hidden" name="article_id"
value="&lt;?php echo $article_id ?&gt;" /&gt;

&lt;input type="submit" value="Save"/&gt;&lt;br /&gt;

&lt;/form&gt;
&lt;div id="timestamp"&gt;&lt;/div&gt;
</code></pre>
<p>Now for our AJAX back end, we must create a script that will perform the update from the AJAX request that came from <span class='code'>add_article.php</span>. Let us call this script <span class='code'>autosave.php</span>. In return, the script must output the timestamp when the article was last updated</p>
<p><b>autosave.php</b>
<pre><code class='php'>$title = mysql_real_escape_string($_POST['title']);
$content = mysql_real_escape_string($_POST['content']);
$id = (int)$_POST['article_id'];

//save contents to database
mysql_query("UPDATE `articles`
    SET title = '$title', content = '$content' WHERE id = '$id'");

//get timestamp
$result = mysql_query("SELECT timestamp FROM `articles`
    WHERE id = $id");
$timestamp = mysql_result($result, 0);

//output timestamp
echo 'Last Saved: ', $timestamp;</code></pre>
<p>And finally to tie it all up, we must now create the autosave script in jQuery on <span class='code'>add_article.php</span>. First of all, in creating our <span class='code'>autosave()</span> function, we will need to create a setTimeout loop that will call itself recursively. For this tutorial, the interval between auto-saving is 20 seconds</p>
<p><b>add_article.php</b>
<pre><code class='javascript'>function autosave()
{
	var t = setTimeout("autosave()", 20000);
}</code></pre>
<p>And finally, to finish it all up, we must send an AJAX request to our back end <span class='code'>autosave.php</span>. Of course, we mustn&#8217;t forget to pass the <span class='code'>$article_id</span> we generated earlier. Also, auto-saving must only proceed when both title and contents are not empty.</p>
<p>And lastly, when the AJAX request has been successfully processed, we will pass its value to our timestamp container.</p>
<p><b>add_article.php</b>
<pre><code class='javascript'>function autosave()
{
	var t = setTimeout("autosave()", 20000);

	var title = $("#txt_title").val();
	var content = $("#txt_content").val();

	if (title.length > 0 || content.length > 0)
	{
		$.ajax(
		{
			type: "POST",
			url: "autosave.php",
			data: "article_id=" + &lt;?php echo $article_id ?&gt;
+ "&#038;title=" + title + "&#038;content=" + content,
			cache: false,
			success: function(message)
			{
				$("#timestamp").empty().append(message);
			}
		});
	}
}</code></pre>
<p>Finally, it is just a matter of starting the auto-saving feature when the DOM is ready:</p>
<p><b>add_article.php</b>
<pre><code class='javascript'>$(document).ready(function(){
	autosave();
});
</code></pre>
<p>Working demo of the tutorial and the download links can be found below:</p>
<p><a href="http://jetlogs.org/jquery/jquery_autosave.php">View jQuery Auto-save Demo</a><br />
<a href="http://jetlogs.org/jquery/jquery_autosave.zip">Download Source Codes</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2007%2F11%2F11%2Fauto-saving-with-jquery%2F&amp;title=Auto-saving+with+jQuery" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2007%2F11%2F11%2Fauto-saving-with-jquery%2F&amp;title=Auto-saving+with+jQuery" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2007%2F11%2F11%2Fauto-saving-with-jquery%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2007%2F11%2F11%2Fauto-saving-with-jquery%2F&amp;title=Auto-saving+with+jQuery" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F11%2F11%2Fauto-saving-with-jquery%2F&amp;title=Auto-saving+with+jQuery" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F11%2F11%2Fauto-saving-with-jquery%2F&amp;title=Auto-saving+with+jQuery" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2007%2F11%2F11%2Fauto-saving-with-jquery%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2007%2F11%2F11%2Fauto-saving-with-jquery%2F&amp;t=Auto-saving+with+jQuery" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2007/11/11/auto-saving-with-jquery/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>jQuery: Auto-suggest with keyup() event</title>
		<link>http://jetlogs.org/2007/10/22/jquery-auto-suggest-with-keyup-event/</link>
		<comments>http://jetlogs.org/2007/10/22/jquery-auto-suggest-with-keyup-event/#comments</comments>
		<pubDate>Mon, 22 Oct 2007 08:59:33 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[onkeyup]]></category>

		<guid isPermaLink="false">http://jetlogs.org/2007/10/22/jquery-auto-suggest-with-keyup-event/</guid>
		<description><![CDATA[Due to a request from a friend, here is a tutorial on how to do an auto-suggest script using jQuery and the keyup() event. The program logic of this script is based on the AJAX suggest script from w3schools but with some improvements on the efficiency, ease of use and customization by using jQuery. The [...]]]></description>
			<content:encoded><![CDATA[<p>Due to a request from a friend, here is a tutorial on how to do an auto-suggest script using jQuery and the keyup() event. The program logic of this script is based on the <a href='http://w3schools.com/ajax/ajax_example_suggest.asp'>AJAX suggest script from w3schools</a> but with some improvements on the efficiency, ease of use and customization by using jQuery.</p>
<p>The basic logic of an auto-suggest script is that for every keystroke that a user enters on a textfield, the script must send an AJAX request and validate which words in a dictionary most closely resembles the current input.<span id="more-268"></span></p>
<p>First of all, we will need to have a textbox field for our search field. Then let&#8217;s give it an id called <span class='code'>txt_search</span> (#txt_search)</p>
<pre><code class='html'>&lt;input type="text" id="txt_search" name="search"&gt;</code></pre>
<p>Next, we will need a <span class='code'>&lt;span&gt;</span> or a <span class='code'>&lt;div&gt;</span> where our auto-suggest results will show up. Let&#8217;s give this an id called <span class='code'>suggest</span> (#suggest)</p>
<pre><code class='html'>&lt;span id="suggest"&gt;&lt;/span&gt;</code></pre>
<p>Now, this is where the keyup() event comes in. For every keystroke that the user makes on the search input box, we must trigger an AJAX request. However, if we do not have any text on the search input box, no AJAX request should be triggered at all. Here is how it should look like:</p>
<pre><code class='javascript'>$("#txt_search").keyup(function()
{
	var search;
	search = $("#txt_search").val();

	if (search.length > 0)
	{
		// Trigger AJAX request
	}
	else
	{
		// Empty suggestion list
		$("#suggest").empty();
	}
});</code></pre>
<p>And finally, to trigger the AJAX request itself, we must call on jQuery&#8217;s $.ajax() function to send the request. In this case, we must make an AJAX request to the backend which will check for the matching suggestions. For this tutorial, we will name our backend <span class='code'>dictionary.php</span> And upon success, we must clear the current contents of the <span class='code'>#suggest</span> container and replace it with the AJAX response:</p>
<pre><code class='javascript'>$.ajax(
{
	type: "POST",
	url: "dictionary.php",
	data: "search=" + search,
	success: function(message)
	{
		$("#suggest").empty();
		if (message.length > 0)
		{
			message = "Do you mean: " + message;
			$("#suggest").append(message);
		}
	}
});</code></pre>
<p>Now the backend&#8217;s job is to search the dictionary for the matching search term. For this tutorial, the dictionary will be in the form of an array. However, the dictionary source can be substituted by a flat file or database results (caution in using DB resources, especially in high-traffic conditions. Use a DB result cache if possible). The output would be a comma-separated list of words that match the search term. Here is how the PHP script would look like:</p>
<pre><code class='php'>&lt;?php
$search = strtolower($_POST['search']);
$search_length = strlen($search);
$suggest = array();

$dictionary = array('C++', 'Smalltalk', ...);

foreach ($dictionary as $word)
{
	if($search == substr(strtolower($word), 0, $search_length))
	{
		$suggest[] = $word;
	}
}

echo implode(', ', $suggest);
?&gt;</code></pre>
<p>In the end, here is how the auto-suggest will look like:<br />
<a href="/jquery/jquery_auto_suggest.html">jQuery Auto-suggest Tutorial Demo</a></p>
<p>You can download the full demo here:<br />
<a href="/jquery/jquery_auto_suggest.zip">Download the jQuery Auto-suggest Tutorial</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2007%2F10%2F22%2Fjquery-auto-suggest-with-keyup-event%2F&amp;title=jQuery%3A+Auto-suggest+with+keyup%28%29+event" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2007%2F10%2F22%2Fjquery-auto-suggest-with-keyup-event%2F&amp;title=jQuery%3A+Auto-suggest+with+keyup%28%29+event" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2007%2F10%2F22%2Fjquery-auto-suggest-with-keyup-event%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2007%2F10%2F22%2Fjquery-auto-suggest-with-keyup-event%2F&amp;title=jQuery%3A+Auto-suggest+with+keyup%28%29+event" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F10%2F22%2Fjquery-auto-suggest-with-keyup-event%2F&amp;title=jQuery%3A+Auto-suggest+with+keyup%28%29+event" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F10%2F22%2Fjquery-auto-suggest-with-keyup-event%2F&amp;title=jQuery%3A+Auto-suggest+with+keyup%28%29+event" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2007%2F10%2F22%2Fjquery-auto-suggest-with-keyup-event%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2007%2F10%2F22%2Fjquery-auto-suggest-with-keyup-event%2F&amp;t=jQuery%3A+Auto-suggest+with+keyup%28%29+event" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2007/10/22/jquery-auto-suggest-with-keyup-event/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>jQuery: Textbox Validation and the blur() Event</title>
		<link>http://jetlogs.org/2007/09/14/jquery-textbox-validation-and-the-blur-event/</link>
		<comments>http://jetlogs.org/2007/09/14/jquery-textbox-validation-and-the-blur-event/#comments</comments>
		<pubDate>Thu, 13 Sep 2007 16:06:03 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[onblur]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://jetlogs.org/2007/09/14/jquery-textbox-validation-and-the-blur-event/</guid>
		<description><![CDATA[Here is another jQuery tutorial demonstrating how to perform text validations using jQuery&#8217;s blur() event. Its a fact that client-side client validation is no alternative to server-side validation especially in web applications. However, that doesn&#8217;t mean that we should drop client-side validation altogether. The truth is that client-side validation enhances a web application. First, the [...]]]></description>
			<content:encoded><![CDATA[<p>Here is another jQuery tutorial demonstrating how to perform text validations using jQuery&#8217;s blur() event. </p>
<p>Its a fact that client-side client validation is no alternative to server-side validation especially in web applications. However, that doesn&#8217;t mean that we should drop client-side validation altogether. The truth is that client-side validation enhances a web application. First, the client-side validations provide a more responsive interface for users. Secondly, for a very large enterprise-grade application, shifting some of the validations on the client as a first pass filter will reduce the load on the server and improve its performance.</p>
<p>Now let&#8217;s move on to the tutorial. First of all, I&#8217;m going to use a simple registration form as an example for validation. Here is the demo for this tutorial:<br />
<script type="text/javascript" src="/jquery/textbox_validation.js"></script></p>
<p><b>Username:</b> <i>Username must be at least 4 characters in length</i></p>
<input type="text" id="txt_username" name="username"> <span id="username_warning" style="color:red"></span></p>
<p><b>Password:</b> <i>Password must be at least 6 characters in length</i><br />
<br />
<input type="password" id="txt_password" name="password"> <span id="password_warning" style="color:red"></span><span id="more-215"></span></p>
<p>Here is how the form looks like in code view:</p>
<pre><code class='html'>&lt;b&gt;Username:&lt;/b&gt; &lt;i&gt;Username must be at least 4 characters in length&lt;/i&gt;
&lt;input type="text" id="txt_username" name="username"&gt;
&lt;span id="username_warning" style="color:red"&gt;&lt;/span&gt;
&lt;b&gt;Password:&lt;/b&gt; &lt;i&gt;Password must be at least 6 characters in length&lt;/i&gt;
&lt;br&gt;&lt;input type="password" id="txt_password" name="password"&gt;
&lt;span id="password_warning" style="color:red"&gt;&lt;/span&gt;</code></pre>
<p>In the example above, we&#8217;ve created a span for where our error message will appear with a unique id so they can be selected later on. What is important in this form is that we assign an <b>id</b> for each of the fields we need to validate so we can bind it with its corresponding jQuery event. Since we want to validate the text boxes once a user has supplied an input, the events of choice here will be both the change() and blur() event. However, the change() event has problems when the user reloads the page. Even with an invalid input, the validation will not trigger unless the user has changed the value of the input itself.</p>
<p>Now to validate the length of the user input, we must find the length of the text fields using the jQuery selectors. Here is how to do it:</p>
<pre><code class='javascript'>$(document).ready(function(){
	$("#txt_username").blur(function()
	{
		var username_length;

		username_length = $("#txt_username").val().length;
		$("#username_warning").empty();

		if (username_length < 4)
			$("#username_warning").append("Username is too short");
	});

	$("#txt_password").blur(function()
	{
		var password_length;

		password_length = $("#txt_password").val().length;
		$("#password_warning").empty();

		if (password_length < 6)
			$("#password_warning").append("Password is too short");
	});
});
</code></pre>
<p>If you want to view the tutorial, you can download it here below:<br />
<a href='/jquery/jquery_textbox_validation.zip'>Textbox Validation and the blur() Event Demo Download</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F14%2Fjquery-textbox-validation-and-the-blur-event%2F&amp;title=jQuery%3A+Textbox+Validation+and+the+blur%28%29+Event" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F14%2Fjquery-textbox-validation-and-the-blur-event%2F&amp;title=jQuery%3A+Textbox+Validation+and+the+blur%28%29+Event" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F14%2Fjquery-textbox-validation-and-the-blur-event%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F14%2Fjquery-textbox-validation-and-the-blur-event%2F&amp;title=jQuery%3A+Textbox+Validation+and+the+blur%28%29+Event" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F14%2Fjquery-textbox-validation-and-the-blur-event%2F&amp;title=jQuery%3A+Textbox+Validation+and+the+blur%28%29+Event" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F14%2Fjquery-textbox-validation-and-the-blur-event%2F&amp;title=jQuery%3A+Textbox+Validation+and+the+blur%28%29+Event" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F14%2Fjquery-textbox-validation-and-the-blur-event%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F14%2Fjquery-textbox-validation-and-the-blur-event%2F&amp;t=jQuery%3A+Textbox+Validation+and+the+blur%28%29+Event" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2007/09/14/jquery-textbox-validation-and-the-blur-event/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>jQuery: Select Boxes and change() events</title>
		<link>http://jetlogs.org/2007/09/03/jquery-select-boxes-and-change-events/</link>
		<comments>http://jetlogs.org/2007/09/03/jquery-select-boxes-and-change-events/#comments</comments>
		<pubDate>Mon, 03 Sep 2007 08:53:54 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[onchange]]></category>

		<guid isPermaLink="false">http://jetlogs.org/2007/09/03/jquery-select-boxes-and-change-events/</guid>
		<description><![CDATA[Here is a very simple jQuery trick to display a message depending on which option is currently selected on a select box. This is very useful especially when there is a need for an explanation or give additional information on some items within a select box. Here is a demo of this tutorial at work: [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a very simple jQuery trick to display a message depending on which option is currently selected on a select box. This is very useful especially when there is a need for an explanation or give additional information on some items within a select box.</p>
<p>Here is a demo of this tutorial at work:<br />
<script type="text/javascript" src="/jquery/select_change.js"></script></p>
<select id="item_select">
<option>Select an Item</option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
<div id="message_display" style="color:#000080"></div>
<p>Quite handy isn&#8217;t it? Here is how to do it in a few simple steps:<span id="more-202"></span></p>
<p>First of all, we need to have our select box. Here is how the select box above looks like in code view</p>
<pre><code class="html">&lt;select id="item_select" name="item"&gt;
	&lt;option&gt;Select an Item&lt;/option&gt;
	&lt;option value="1"&gt;Item 1&lt;/option&gt;
	&lt;option value="2"&gt;Item 2&lt;/option&gt;
	&lt;option value="3"&gt;Item 3&lt;/option&gt;
&lt;/select&gt;</code></pre>
<p>For our demo, we will give the select box an id called &#8220;item_select&#8221; (#item_select) so we can access it later on via jQuery.The values for each option is going to be important as we move on.</p>
<p>The next step will now be creating an array of messages that will correspond to the values of each option. The easiest way to do this is by using the values of each option as the array&#8217;s index. Here is how to do it. Place this within the &lt;script&gt; tags:</p>
<pre><code class="javascript">var message = new Array();
message[1] = "You have selected first item";
message[2] = "You have selected the second item";
message[3] = "You have selected the last item";</code></pre>
<p>Now, the only thing missing is the container in which message will appear. Let&#8217;s create a div with an id called &#8220;message_display&#8221; for this purpose:</p>
<pre><code class="html">&lt;div id="message_display"&gt;&lt;/div&gt;</code></pre>
<p>Now for the final piece of the puzzle: To show our message when a user selects an option, we must assign the change() event of #item_select with the message display. Then we must assign the value of the option selected as the index of the message we want to display. Here is how to do it:</p>
<pre><code class="javascript">&lt;script type="text/javascript"&gt;
var message = new Array();
message[1] = "You have selected first item";
message[2] = "You have selected the second item";
message[3] = "You have selected the last item";

$(document).ready(function(){
	$("#item_select").change(function()
	{
		var message_index

		message_index = $("#item_select").val();
		$("#message_display").empty();

		if (message_index > 0)
			$("#message_display").append(message[message_index]);
	});
});
&lt;/script&gt;</code></pre>
<p>The important thing to note here is that we must empty() the #message_display div or else the messages will keep on stacking due to the append() command.</p>
<p>That is all for the tutorial for today.</p>
<p>You can download the sample demo here:<br />
<a href="/jquery/jquery_select_change.zip">Select Boxes and change() event demo</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F03%2Fjquery-select-boxes-and-change-events%2F&amp;title=jQuery%3A+Select+Boxes+and+change%28%29+events" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F03%2Fjquery-select-boxes-and-change-events%2F&amp;title=jQuery%3A+Select+Boxes+and+change%28%29+events" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F03%2Fjquery-select-boxes-and-change-events%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F03%2Fjquery-select-boxes-and-change-events%2F&amp;title=jQuery%3A+Select+Boxes+and+change%28%29+events" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F03%2Fjquery-select-boxes-and-change-events%2F&amp;title=jQuery%3A+Select+Boxes+and+change%28%29+events" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F03%2Fjquery-select-boxes-and-change-events%2F&amp;title=jQuery%3A+Select+Boxes+and+change%28%29+events" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F03%2Fjquery-select-boxes-and-change-events%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2007%2F09%2F03%2Fjquery-select-boxes-and-change-events%2F&amp;t=jQuery%3A+Select+Boxes+and+change%28%29+events" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2007/09/03/jquery-select-boxes-and-change-events/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>jQuery: Select All Checkbox</title>
		<link>http://jetlogs.org/2007/08/01/jquery-select-all-checkbox/</link>
		<comments>http://jetlogs.org/2007/08/01/jquery-select-all-checkbox/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 15:52:57 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://jetlogs.org/2007/08/01/jquery-select-all-checkbox/</guid>
		<description><![CDATA[Note: [@attr] selectors are no longer supported by jQuery 1.3.x and above. Use [attr] instead Here is a simple tutorial on how to implement the &#8220;select all&#8221; function for checkboxes in jQuery. All you need to know for this tutorial is the basic knowledge of using the jQuery library. assuming we have included the jQuery [...]]]></description>
			<content:encoded><![CDATA[<p><b>Note: [@attr] selectors are no longer supported by jQuery 1.3.x and above. Use [attr] instead</b></p>
<p>Here is a simple tutorial on how to implement the &#8220;select all&#8221; function for checkboxes in jQuery. All you need to know for this tutorial is the basic knowledge of using the <a href="http://jquery.com/">jQuery</a> library.</p>
<p>assuming we have included the jQuery library in our document:</p>
<pre><code class='html'>
&lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;
</code></pre>
<p>The next step would be<span id="more-183"></span> defining our form. For our example, we&#8217;ll just use a very simple checkbox list to select which paradigms are applicable to Javascript. We&#8217;ll name these checkboxes simply as <span class='code'>&#8220;paradigm&#8221;</span>:</p>
<pre><code class='html'>
&lt;input type="checkbox" name="paradigm"
value="Imperative"&gt;Imperative&lt;br&gt;
&lt;input type="checkbox" name="paradigm"
value="Object-Oriented"&gt;Object-Oriented&lt;br&gt;
&lt;input type="checkbox" name="paradigm"
value="Functional"&gt;Functional&lt;br&gt;
</code></pre>
<p>and of course, we need the &#8220;select all&#8221; checkbox to perform the operation. We&#8217;ll give this checkbox an id name of <span class='code'>paradigm_all</span>:</p>
<pre><code class='html'>
&lt;input type="checkbox" id="paradigm_all"&gt;Select All
</code></pre>
<p>Now lets go to the main part of jQuery code. What happens when we select the &#8220;select all&#8221; checkbox is that the checkbox&#8217;s status will be passed to all of the checkbox list. For example, if the &#8220;select all&#8221; checkbox is checked, all checkboxes should also be checked, and vice versa. </p>
<p>So how do we select all of the checkboxes to change their checked state? Fortuantely, jQuery has XPath style selectors which allows us to choose multiple elements in the DOM. the expression:</p>
<pre><code class='javascript'>input[@name=paradigm]</code></pre>
<p>will select ALL input elements that are named paradigm. Here is how the final jQuery script should look like:</p>
<pre><code class='javascript'>
$(document).ready(function()
		{
			$("#paradigm_all").click(function()
			{
				var checked_status = this.checked;
				$("input[@name=paradigm]").each(function()
				{
					this.checked = checked_status;
				});
			});
		});
</code></pre>
<p>If you want to view a demo of the following script or to download the demo source code, just follow the links below.</p>
<p><a href="/jquery/jquery_select_all.html">View Demo</a><br />
<a href="/jquery/jquery_select_all.zip">Download Source Code</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2007%2F08%2F01%2Fjquery-select-all-checkbox%2F&amp;title=jQuery%3A+Select+All+Checkbox" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2007%2F08%2F01%2Fjquery-select-all-checkbox%2F&amp;title=jQuery%3A+Select+All+Checkbox" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2007%2F08%2F01%2Fjquery-select-all-checkbox%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2007%2F08%2F01%2Fjquery-select-all-checkbox%2F&amp;title=jQuery%3A+Select+All+Checkbox" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F08%2F01%2Fjquery-select-all-checkbox%2F&amp;title=jQuery%3A+Select+All+Checkbox" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F08%2F01%2Fjquery-select-all-checkbox%2F&amp;title=jQuery%3A+Select+All+Checkbox" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2007%2F08%2F01%2Fjquery-select-all-checkbox%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2007%2F08%2F01%2Fjquery-select-all-checkbox%2F&amp;t=jQuery%3A+Select+All+Checkbox" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2007/08/01/jquery-select-all-checkbox/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>jQuery: Input Validation with Form Plugin</title>
		<link>http://jetlogs.org/2007/07/22/jquery-input-validation-with-form-plugin/</link>
		<comments>http://jetlogs.org/2007/07/22/jquery-input-validation-with-form-plugin/#comments</comments>
		<pubDate>Sat, 21 Jul 2007 17:26:45 +0000</pubDate>
		<dc:creator>Jetlogs</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://jetlogs.org/2007/07/22/jquery-input-validation-with-form-plugin/</guid>
		<description><![CDATA[In this next tutorial, I will demonstrate how to perform input validation on AJAX forms using the jQuery form plugin. The requirements for this tutorial are: Basic knowledge of jQuery Basic knowledge of jQuery form plugin usage Basic knowledge of javascript and the required files for this tutorial are: jQuery library jQuery form plugin Before [...]]]></description>
			<content:encoded><![CDATA[<p>In this next tutorial, I will demonstrate how to perform input validation on AJAX forms using the jQuery form plugin.</p>
<p>The requirements for this tutorial are:</p>
<li>Basic knowledge of jQuery</li>
<li>Basic knowledge of jQuery form plugin usage</li>
<li>Basic knowledge of javascript</li>
<p>and the required files for this tutorial are:</p>
<li><a href="http://jquery.com">jQuery library</a></li>
<li><a href="http://www.malsup.com/jquery/form/">jQuery form plugin</a></li>
<p>Before we start with this tutorial, let me make one thing clear:</p>
<p><b>Client-side input validation is no substitute for server-side validation.</b><br />
Client-side validation can be easily bypassed, and should be treated more as an enhancement for the form.</p>
<p>now let us continue with the tutorial<span id="more-176"></span><br />
For this tutorial, I&#8217;m going to use a very simple product information form. here is how it would look like:</p>
<pre><code class='html'>	&lt;b&gt;Enter Product Information:&lt;/b&gt;&lt;br&gt;&lt;br&gt;
	&lt;form id="prod_form" method="POST" action="process_product.php"&gt;
		Product Name* :&lt;br&gt;
		&lt;input type="text" name="prod_name" id="prod_name"&gt;
		&lt;span class="error" id="prod_name_error"&gt;&lt;/span&gt;&lt;br&gt;
		Quantity* :&lt;br&gt;
		&lt;input type="text" name="prod_quantity" id="prod_quantity"&gt;
		&lt;span class="error" id="prod_quantity_error"&gt;&lt;/span&gt;&lt;br&gt;
		Optional Description:&lt;br&gt;
		&lt;textarea name="prod_description" cols="30" rows="3"&gt;&lt;/textarea&gt;
		&lt;br&gt;&lt;br&gt;

		&lt;input type="submit" value='Submit'&gt;
	&lt;/form&gt;
</code></pre>
<p>In our form, the product name and the product quantity would be the required fields. Also the quantity should be a valid numeric integer. For now, we&#8217;ll give our for the id name <span class="code">prod_form</span>. Also notice that a blank <span class='code'>&lt;span&gt;</span> for the error messages has already been defined for each required field? We&#8217;ll get to that later on </p>
<p>So how do we implement this server side validation in jQuery? With the jQuery Form Plugin, it is much easier to process the validations.</p>
<p>First of all, we must define the jQuery base, and the jQuery form plugin at the head of the document.</p>
<pre><code class='html'>&lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="jquery.form.js"&gt;&lt;/script&gt;
</code></pre>
<p>now for the most important part, we must set a pre-submit callback function in the ajaxForm options. Here is how to do it:</p>
<pre><code class='javascript'>$('#prod_form').ajaxForm({
					target: 		'#content',
					beforeSubmit: 	validateForm
			});</code></pre>
<p>The <span class='code'>target</span> option basically states where the AJAX output will display, while the <span class='code'>beforeSubmit</span> option shall contain the function that will be processed before the form is submitted to the server.</p>
<p>In our form, we&#8217;ve set a function named <span class='code'>validateForm</span> to handle the validations. Here is what the <span class='code'>validateForm</span> looks like:</p>
<pre><code class='javascript'>function validateForm()
{
	$("#prod_name_error").empty().hide();
	$("#prod_quantity_error").empty().hide();

	var product_name 		= $("#prod_name").val();
	var product_quantity	= $("#prod_quantity").val();

	var errors 				= 0;

	if (product_name == null || product_name == '')
	{
		$("#prod_name_error").show()
.append("Product Name is required");
		errors++;
	}
	if (product_quantity == null || product_quantity == '')
	{
		$("#prod_quantity_error").show()
.append("Product Quantity is required");
		errors++;
	}
	else if (!isNumeric(product_quantity))
	{
		$("#prod_quantity_error").show()
.append("Product Quantity should be numeric");
		errors++;
	}

	if (errors > 0)
	{
		alert ("Errors were found on the form");
		return false;
	}

}		

function isNumeric(form_value)
{
	if (form_value.match(/^\d+$/) == null)
		return false;
	else
		return true;
}
</code></pre>
<p>Here is where all the validation happens. The first couple of if-then blocks basically checks if a value has been entered for for the required fields: <span class='code'>product_name</span> and <span class='code'>product_quantity</span>. And the last if-then block checks whether <span class='code'>product_quantity</span> is a valid numeric integer through a regex function.</p>
<p>Also regarding the <span class='code'>beforeSubmit</span> callback function, if it returns <span class='code'>FALSE</span>, the form shall not be submitted.</p>
<p>Also, this code shows how to display the error message for each individual required field. What the <span class='code'>validateForm</span> function does is that it appends the error warning on the <span class='code'>&lt;span&gt;</span> we have defined earlier. </p>
<p>If you want to see this tutorial in action, just check on the links below</p>
<p><a href="http://jetlogs.org/jquery/jquery_form_validation.php">View Input Validation Demo</a><br />
<a href="http://jetlogs.org/jquery/jquery_form_validation.zip">Download Source Code</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fjetlogs.org%2F2007%2F07%2F22%2Fjquery-input-validation-with-form-plugin%2F&amp;title=jQuery%3A+Input+Validation+with+Form+Plugin" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fjetlogs.org%2F2007%2F07%2F22%2Fjquery-input-validation-with-form-plugin%2F&amp;title=jQuery%3A+Input+Validation+with+Form+Plugin" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fjetlogs.org%2F2007%2F07%2F22%2Fjquery-input-validation-with-form-plugin%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fjetlogs.org%2F2007%2F07%2F22%2Fjquery-input-validation-with-form-plugin%2F&amp;title=jQuery%3A+Input+Validation+with+Form+Plugin" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F07%2F22%2Fjquery-input-validation-with-form-plugin%2F&amp;title=jQuery%3A+Input+Validation+with+Form+Plugin" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fjetlogs.org%2F2007%2F07%2F22%2Fjquery-input-validation-with-form-plugin%2F&amp;title=jQuery%3A+Input+Validation+with+Form+Plugin" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fjetlogs.org%2F2007%2F07%2F22%2Fjquery-input-validation-with-form-plugin%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fjetlogs.org%2F2007%2F07%2F22%2Fjquery-input-validation-with-form-plugin%2F&amp;t=jQuery%3A+Input+Validation+with+Form+Plugin" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://jetlogs.org/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://jetlogs.org/2007/07/22/jquery-input-validation-with-form-plugin/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

