<?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>ViralPatel.net &#187; JQuery</title> <atom:link href="http://viralpatel.net/blogs/category/javascript/jquery/feed" rel="self" type="application/rss+xml" /><link>http://viralpatel.net/blogs</link> <description>Tutorials, Java, J2EE, Struts, AJAX, JavaScript, CSS, Web 2.0, MySQL, Articles</description> <lastBuildDate>Tue, 24 Jan 2012 13:45:10 +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>Facebook Style Scroll Fixed Header in JQuery</title><link>http://viralpatel.net/blogs/2012/01/scroll-fix-header-jquery-facebook.html</link> <comments>http://viralpatel.net/blogs/2012/01/scroll-fix-header-jquery-facebook.html#comments</comments> <pubDate>Wed, 11 Jan 2012 12:22:41 +0000</pubDate> <dc:creator>Viral Patel</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[JQuery]]></category> <category><![CDATA[facebook]]></category> <category><![CDATA[header]]></category> <category><![CDATA[JQuery Events]]></category> <category><![CDATA[mouse scroll event]]></category><guid isPermaLink="false">http://viralpatel.net/blogs/?p=2613</guid> <description><![CDATA[While doing some UI changes of a website, we had to implement a FIX header which remains fix on top of screen while scrolling. Facebook has a similar header which remains on top of content. Now for this, there are number of jQuery plugins available out there! But in our case we weren&#8217;t allowed to [...]]]></description> <content:encoded><![CDATA[<p>While doing some UI changes of a website, we had to implement a FIX header which remains fix on top of screen while scrolling. Facebook has a similar header which remains on top of content.</p><p>Now for this, there are number of jQuery plugins available out there! But in our case we weren&#8217;t allowed to add any new plugins to the technology stack of application (I know it sucks <img src='http://viralpatel.net/blogs/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> ). So here is a small JQuery based solution to make a DIV of header fixed on top of the screen while scrolling.</p><p><strong>Related:</strong> <a href="http://viralpatel.net/blogs/2009/08/javascript-mouse-scroll-event-down-example.html">Mouse Scroll Events in JQuery</a></p><h2>The HTML</h2><p>Below is the HTML code. It contain a simple div #header which we want to freeze at the top. Note that we have included JQuery directly from jquery.com site (In my application we have included older version of jquery from out tech stack).</p><pre class="brush: xml; highlight: [8]; title: ; notranslate">
&lt;HTML&gt;
&lt;HEAD&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;http://code.jquery.com/jquery-latest.js&quot;&gt;&lt;/script&gt;
	&lt;title&gt;Scroll Fixed Header like Facebook in JQuery - viralpatel.net&lt;/title&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;

&lt;div id=&quot;header&quot;&gt;
	&lt;h1&gt;Scroll Fix Header like Facebook in JQuery&lt;/h1&gt;
&lt;/div&gt;

&lt;p&gt;Some dumb text to add scrolling in page &lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;p&gt;Some more dumb text to add scrolling in page &lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;p&gt;Some more dumb text to add scrolling in page &lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;p&gt;Some more dumb text to add scrolling in page &lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;

&lt;/BODY&gt;
&lt;/HTML&gt;
</pre><p>In above HTML, we added few lines &#8220;Some dumb text to add&#8230;&#8221; just to make page scrollable.</p><h2>The JQuery</h2><p>The JQuery code is pretty straight forward. Take a look:</p><pre class="brush: jscript; highlight: [4]; title: ; notranslate">
&lt;SCRIPT&gt;
$(document).ready(function() {

	var div = $('#header');
	var start = $(div).offset().top;

	$.event.add(window, &quot;scroll&quot;, function() {
		var p = $(window).scrollTop();
		$(div).css('position',((p)&gt;start) ? 'fixed' : 'static');
		$(div).css('top',((p)&gt;start) ? '0px' : '');
	});

});
&lt;/SCRIPT&gt;
</pre><p>We have added an event listener to &#8220;<strong>scroll</strong>&#8221; event. This handler function gets called each time scroll event is fired in browser.</p><p>Now we select the header element (highlighted in above code) and simply do some position stuff. We make the header div&#8217;s position <strong>fixed</strong> of <strong>static</strong> depending upon the state of scroll position. Also the <strong>top</strong> attribute is set to 0px in case we want to make it fix, when page is scrolled down.</p><h2>Online Demo</h2><style>.demobtns
a{background:-moz-linear-gradient(center top , #BDED82, #83CC29) repeat scroll 0 0 transparent;border:1px
solid #5F961A;border-radius:7px 7px 7px 7px;color:#5F961A;display:inline-block;font-size:18px;margin-right:5px;padding:10px
9px}</style><div class="demobtns"> <a target="_new" href="http://viralpatel.net/blogs/demo/jquery/scroll-fix-header-facebook/">Demo</a> <a target="_new" href="http://viralpatel.net/blogs/demo/jquery/scroll-fix-header-facebook/download.zip">Download</a></div><p><br/></p><div id="relatedpost"><h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://viralpatel.net/blogs/2009/12/jquery-live-events.html" title="jQuery Live Event">jQuery Live Event</a></li><li><a href="http://viralpatel.net/blogs/2009/04/tutorial-handle-browser-events-using-jquery-javascript-framework.html" title="Tutorial: Handle browser events using jQuery JavaScript framework">Tutorial: Handle browser events using jQuery JavaScript framework</a></li><li><a href="http://viralpatel.net/blogs/2011/02/clearable-textbox-jquery.html" title="Create a Clearable TextBox with jQuery">Create a Clearable TextBox with jQuery</a></li><li><a href="http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html" title="jQuery: Get the Text of Element without Child Element">jQuery: Get the Text of Element without Child Element</a></li><li><a href="http://viralpatel.net/blogs/2011/01/first-play-framework-gae-siena-application-tutorial-example.html" title="Your first Play! &#8211; GAE &#8211; Siena application: Tutorial with Example">Your first Play! &#8211; GAE &#8211; Siena application: Tutorial with Example</a></li><li><a href="http://viralpatel.net/blogs/2010/12/dynamically-shortened-text-show-more-link-jquery.html" title="Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery">Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery</a></li><li><a href="http://viralpatel.net/blogs/2010/12/jquery-ajax-handling-unauthenticated-request-ajax.html" title="jQuery Ajax &#8211; Handling unauthenticated requests via Ajax">jQuery Ajax &#8211; Handling unauthenticated requests via Ajax</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://viralpatel.net/blogs/2012/01/scroll-fix-header-jquery-facebook.html/feed</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Create a Clearable TextBox with jQuery</title><link>http://viralpatel.net/blogs/2011/02/clearable-textbox-jquery.html</link> <comments>http://viralpatel.net/blogs/2011/02/clearable-textbox-jquery.html#comments</comments> <pubDate>Wed, 23 Feb 2011 23:50:54 +0000</pubDate> <dc:creator>Viral Patel</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[JQuery]]></category> <category><![CDATA[jquery tips tricks]]></category> <category><![CDATA[jquery ui tutorial]]></category><guid isPermaLink="false">http://viralpatel.net/blogs/?p=2159</guid> <description><![CDATA[Recently I came across a wonderful article by David Walsh on Clearable Textboxes with Dojo Toolkit [link]. This is a simple implementation where a clear button (x) added in a textbox which lets user to clear the content of the textbox. I have seen this simple feature in many websites and feel its very useful [...]]]></description> <content:encoded><![CDATA[<p><img src="http://img.viralpatel.net/2011/02/clearable-textbox-jquery.png" alt="clearable-textbox-jquery" title="clearable-textbox-jquery" width="335" height="46" class="aligncenter size-full wp-image-2160" /><br /> <br /> Recently I came across a wonderful article by David Walsh on Clearable Textboxes with Dojo Toolkit [<a rel="nofollow" target="_new" href="http://davidwalsh.name/dojo-textbox">link</a>]. This is a simple implementation where a clear button (x) added in a textbox which lets user to clear the content of the textbox. I have seen this simple feature in many websites and feel its very useful to have. It increase the usability of form.</p><p>I thought to implement the same feature using jQuery. Here is a simple plugin that I wrote to add clearable feature to any textbox in your HTML form. All you need is to call clearable() method on the textbox. For example.</p><pre class="brush: xml; title: ; notranslate">
...
...
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.clearable.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;jquery.clearable.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;

...
...

&lt;input type=&quot;text&quot;  class=&quot;foo&quot;&gt;&lt;/input&gt;

&lt;script&gt;
$(document).ready(function() {
	$('.foo').clearable();
});
&lt;/scrip&gt;
</pre><p>In above example we included jquery.clearable plugin javascript and stylesheet files. Then called .clearable() method on the textboxes we want to add clearable feature. That&#8217;s it <img src='http://viralpatel.net/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p><p>Let us go through the code and see what exactly goes behind the scene in jquery clearable plugin.</p><h2>The CSS</h2><p>We use following stylesheet classes internally to display the</p><pre class="brush: css; title: ; notranslate">
.divclearable {
	border: 1px solid #888;
	display: -moz-inline-stack;
	display: inline-block;
	zoom:1;
	*display:inline;
	padding-right:5px;
	vertical-align:middle;
}

a.clearlink {
	background: url(&quot;close-button.png&quot;) no-repeat scroll 0 0 transparent;
	background-position: center center;
	cursor: pointer;
	display: -moz-inline-stack;
	display: inline-block;
	zoom:1;
	*display:inline;
	height: 12px;
	width: 12px;
	z-index: 2000;
	border: 0px solid;
}
</pre><p>There are two css classes we uses in clearable plugin. First one is for a wrapper DIV which wraps around the textbox and clear button (x). And the style class is for clear button (x).</p><h2>The JavaScript</h2><p>The important bit of the plugin is javascript code that creates clearable effect. For everytextbox, we wrap it around a DIV container and also add one clear button (x) in this container DIV.</p><p>Here is the jQuery code:</p><pre class="brush: jscript; title: ; notranslate">
	$(this).css({'border-width': '0px', 'outline': 'none'})
		.wrap('&lt;div id=&quot;sq&quot; class=&quot;divclearable&quot;&gt;&lt;/div&gt;')
		.parent()
		.attr('class', $(this).attr('class') + ' divclearable')
		.append('&lt;a class=&quot;clearlink&quot; href=&quot;javascript:&quot;&gt;&lt;/a&gt;');

	$('.clearlink')
		.attr('title', 'Click to clear this textbox')
		.click(function() {

			$(this).prev().val('').focus();

	});
</pre><p>In above javascript code, we do:</p><ol><li>Remove border and outline of the textbox where we want to add clearable feature</li><li>Create a DIV wrapper and wrap the textbox with that DIV</li><li>Add the textbox style class to DIV, so the border / background etc that are specified on textbox are added to DIV</li><li>Create a link for clear text(x) and append in the DIV</li></ol><p>Also for each clear text link (x) we add an onclick handler where we clear the corresponding textbox and set focus in it.</p><h2>Try Demo</h2><p><a target="_new" href="http://viralpatel.net/blogs/demo/jquery/clearable-textboxes/"><b>View demo</b></a><br /> <br /> <iframe src="http://viralpatel.net/blogs/demo/jquery/clearable-textboxes/demo.html" width="100%" height="250px"></iframe></p><h2>Download jQuery Clearable Plugin</h2><p><a href="http://viralpatel.net/blogs/demo/jquery/clearable-textboxes/jquery.clearable.zip">Clearable plugin (zip, 4kb)</a></p><div id="relatedpost"><h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html" title="jQuery: Get the Text of Element without Child Element">jQuery: Get the Text of Element without Child Element</a></li><li><a href="http://viralpatel.net/blogs/2010/11/multiple-checkbox-select-deselect-jquery-tutorial-example.html" title="Multiple Checkbox Select/Deselect using jQuery &#8211; Tutorial with Example">Multiple Checkbox Select/Deselect using jQuery &#8211; Tutorial with Example</a></li><li><a href="http://viralpatel.net/blogs/2009/09/default-text-label-textbox-javascript-jquery.html" title="Default Text Label in Textbox using JavaScript/jQuery">Default Text Label in Textbox using JavaScript/jQuery</a></li><li><a href="http://viralpatel.net/blogs/2009/09/create-accordion-menu-jquery.html" title="Create Simplest Accordion Menu using jQuery">Create Simplest Accordion Menu using jQuery</a></li><li><a href="http://viralpatel.net/blogs/2009/08/20-top-jquery-tips-tricks-for-jquery-programmers.html" title="20 Top jQuery Tips &#038; Tricks for jQuery Programmers">20 Top jQuery Tips &#038; Tricks for jQuery Programmers</a></li><li><a href="http://viralpatel.net/blogs/2009/05/implement-drag-and-drop-example-jquery-javascript-html.html" title="Drag and Drop Example using jQuery JavaScript in HTML">Drag and Drop Example using jQuery JavaScript in HTML</a></li><li><a href="http://viralpatel.net/blogs/2009/04/jquery-tabs-create-html-tabs-using-jquery-ui.html" title="jQuery tabs: Create HTML tabs using jQuery UI">jQuery tabs: Create HTML tabs using jQuery UI</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://viralpatel.net/blogs/2011/02/clearable-textbox-jquery.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>jQuery: Get the Text of Element without Child Element</title><link>http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html</link> <comments>http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html#comments</comments> <pubDate>Wed, 02 Feb 2011 11:30:46 +0000</pubDate> <dc:creator>Viral Patel</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[JQuery]]></category> <category><![CDATA[How-To]]></category> <category><![CDATA[jquery plugin]]></category> <category><![CDATA[jquery tips tricks]]></category><guid isPermaLink="false">http://viralpatel.net/blogs/?p=2153</guid> <description><![CDATA[While writing code in jQuery, I often use .text() method to get the content of any element from DOM. This function is straight forward, it strips off all the html element from a selected element and returns the remaining text. So for below element the .text() method will return &#8220;A quick brown fox&#8221;. But by [...]]]></description> <content:encoded><![CDATA[<p><img src="http://img.viralpatel.net/jquery-logo.png" alt="jquery-logo" title="jquery-logo" width="236" height="85" class="alignleft size-full wp-image-1108" />While writing code in jQuery, I often use <code>.text()</code> method to get the content of any element from DOM. This function is straight forward, it strips off all the html element from a selected element and returns the remaining text.</p><p>So for below element the .text() method will return &#8220;A quick brown fox&#8221;.</p><pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;foo&quot;&gt;
	A quick brown fox
&lt;/div&gt;
&lt;script&gt;
	$(&quot;#foo&quot;).text(); //return &quot;A quick brown fox&quot;
&lt;/script&gt;
</pre><p>But by the definition the .text() returns all the text inside an element. What if you have an element which contains child elements and you want text of only the selected element ignoring all the child elements?</p><p>For example: Here we have a DIV which has text &#8220;A quick brown fox&#8221; and also contain a child DIV which has text &#8220;jumps over a lazy dog!&#8221;</p><pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;foo&quot;&gt;
	A quick brown fox
	&lt;div id=&quot;bar&quot;&gt;
		jumps over a lazy dog!
	&lt;/div&gt;
&lt;/div&gt;

&lt;script&gt;
	$(&quot;#foo&quot;).text(); //return &quot;A quick brown fox jumps over a lazy dog!&quot;
&lt;/script&gt;
</pre><p>What if you just want &#8220;A quick brown fox&#8221; and wants to ignore all the child element? Well, here is a simple solution using jQuery.</p><pre class="brush: jscript; title: ; notranslate">
&lt;script&gt;
	$(&quot;#foo&quot;)
		.clone()	//clone the element
		.children()	//select all the children
		.remove()	//remove all the children
		.end()	//again go back to selected element
		.text();	//get the text of element
&lt;/script&gt;
</pre><p>We clone the element that you want text of, then remove all the child elements from this cloned element and then returns the text of element.</p><p>I have written a small jQuery plugin &#8220;<strong>justtext</strong>&#8221; which you can use to get the text of element and ignore all child elements. Here is the code of <strong>justtext</strong> plugin.</p><pre class="brush: jscript; title: ; notranslate">
jQuery.fn.justtext = function() {

	return $(this)	.clone()
			.children()
			.remove()
			.end()
			.text();

};
</pre><p>So the usage is just by calling <code>justtext()</code> method on any element.</p><pre class="brush: xml; title: ; notranslate">
&lt;!-- include justtext jquery plugin --&gt;
&lt;script src=&quot;http://viralpatel.net/blogs/demo/jquery/get-text-without-child-element/jquery.justtext.1.0.js&quot;&gt;&lt;/script&gt;

&lt;div id=&quot;foo&quot;&gt;
	A quick brown fox
	&lt;div id=&quot;bar&quot;&gt;
		jumps over a lazy dog!
	&lt;/div&gt;
&lt;/div&gt;

&lt;script&gt;
	$(&quot;#foo&quot;).justtext(); //return &quot;A quick brown fox&quot;
&lt;/script&gt;
</pre><h2>Online Demo</h2><p><a target="_new" href="http://viralpatel.net/blogs/demo/jquery/get-text-without-child-element/">Online Demo Link</a><br /> <iframe src="http://viralpatel.net/blogs/demo/jquery/get-text-without-child-element/demo.html" width="100%" height="250px"></iframe></p><div id="relatedpost"><h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://viralpatel.net/blogs/2010/11/multiple-checkbox-select-deselect-jquery-tutorial-example.html" title="Multiple Checkbox Select/Deselect using jQuery &#8211; Tutorial with Example">Multiple Checkbox Select/Deselect using jQuery &#8211; Tutorial with Example</a></li><li><a href="http://viralpatel.net/blogs/2011/02/clearable-textbox-jquery.html" title="Create a Clearable TextBox with jQuery">Create a Clearable TextBox with jQuery</a></li><li><a href="http://viralpatel.net/blogs/2010/12/dynamically-shortened-text-show-more-link-jquery.html" title="Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery">Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery</a></li><li><a href="http://viralpatel.net/blogs/2010/01/jquery-resizable-draggable-resize-drag-tutorial-example.html" title="jQuery Resizable and Draggable Tutorial with Example">jQuery Resizable and Draggable Tutorial with Example</a></li><li><a href="http://viralpatel.net/blogs/2009/09/create-accordion-menu-jquery.html" title="Create Simplest Accordion Menu using jQuery">Create Simplest Accordion Menu using jQuery</a></li><li><a href="http://viralpatel.net/blogs/2009/08/20-top-jquery-tips-tricks-for-jquery-programmers.html" title="20 Top jQuery Tips &#038; Tricks for jQuery Programmers">20 Top jQuery Tips &#038; Tricks for jQuery Programmers</a></li><li><a href="http://viralpatel.net/blogs/2009/06/textarea-resize-javascript-jquery-plugin-resize-textarea-html.html" title="Textarea Resize JavaScript: Resize textarea using jQuery plugin">Textarea Resize JavaScript: Resize textarea using jQuery plugin</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html/feed</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery</title><link>http://viralpatel.net/blogs/2010/12/dynamically-shortened-text-show-more-link-jquery.html</link> <comments>http://viralpatel.net/blogs/2010/12/dynamically-shortened-text-show-more-link-jquery.html#comments</comments> <pubDate>Tue, 28 Dec 2010 12:40:07 +0000</pubDate> <dc:creator>Viral Patel</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[JavaScript]]></category> <category><![CDATA[JQuery]]></category> <category><![CDATA[How-To]]></category><guid isPermaLink="false">http://viralpatel.net/blogs/?p=2126</guid> <description><![CDATA[Facebook user updates has a great functionality. If the comment text is larger than few characters, the extra words are hide and a show more link is presented to user. This way you can keep long text out of the view to user and stop the cluttering of page. Also interested users can click on [...]]]></description> <content:encoded><![CDATA[<p><img src="http://img.viralpatel.net/2010/12/show-more-link.png" alt="" title="show-more-link" width="341" height="82" class="aligncenter size-full wp-image-2127" /><br /> Facebook user updates has a great functionality. If the comment text is larger than few characters, the extra words are hide and a show more link is presented to user. This way you can keep long text out of the view to user and stop the cluttering of page. Also interested users can click on <em>more</em> link and see the full content.</p><p>Here is a simple tutorial to achieve this using jQuery / Javascript.</p><h2>The HTML</h2><p>Below is the sample text. Each text is wrapped in a DIV tag. Note that we have added a class &#8220;more&#8221; in each div. This class will decide if a text needs to be shortened and show more link showed or not.</p><pre class="brush: xml; title: ; notranslate">
&lt;div class=&quot;comment more&quot;&gt;
	Lorem ipsum dolor sit amet, consectetur adipiscing elit.
	Vestibulum laoreet, nunc eget laoreet sagittis,
	quam ligula sodales orci, congue imperdiet eros tortor ac lectus.
	Duis eget nisl orci. Aliquam mattis purus non mauris
	blandit id luctus felis convallis.
	Integer varius egestas vestibulum.
	Nullam a dolor arcu, ac tempor elit. Donec.
&lt;/div&gt;
&lt;div class=&quot;comment more&quot;&gt;
	Duis nisl nibh, egestas at fermentum at, viverra et purus.
	Maecenas lobortis odio id sapien facilisis elementum.
	Curabitur et magna justo, et gravida augue.
	Sed tristique pellentesque arcu quis tempor.
&lt;/div&gt;
&lt;div class=&quot;comment more&quot;&gt;
	consectetur adipiscing elit. Proin blandit nunc sed sem dictum id feugiat quam blandit.
	Donec nec sem sed arcu interdum commodo ac ac diam. Donec consequat semper rutrum.
	Vestibulum et mauris elit. Vestibulum mauris lacus, ultricies.
&lt;/div&gt;
</pre><h2>The CSS</h2><p>Below is the CSS code for our example. Note the class &#8220;.morecontent span&#8221; is hidden. The extra text from the content is wrapped in this span and is hidden at time of page loading.</p><pre class="brush: css; title: ; notranslate">
a {
	color: #0254EB
}
a:visited {
	color: #0254EB
}
a.morelink {
	text-decoration:none;
	outline: none;
}
.morecontent span {
	display: none;
}
.comment {
	width: 400px;
	background-color: #f0f0f0;
	margin: 10px;
}
</pre><h2>The Javascript</h2><p>Below is the Javascript code which iterate through each DIV tags with class &#8220;more&#8221; and split the text in two. First half is showed to user and second is made hidden with a link &#8220;more..&#8221;.</p><p>You can change the behaviour by changing following js variables.</p><ul><li><em>showChar</em>: Total characters to show to user. If the content is more then showChar, it will be split into two halves and first one will be showed to user.</li><li><em>ellipsestext</em>: The text displayed before &#8220;more&#8221; link. Default is &#8220;&#8230;&#8221;</li><li><em>moretext</em>: The text shown in more link. Default is &#8220;more&#8221;. You can change to &#8220;>>&#8221;</li><li><em>lesstext</em>: The text shown in less link. Default is &#8220;less&#8221;. You can change to &#8220;<<"</li></ul><pre class="brush: jscript; title: ; notranslate">
$(document).ready(function() {
	var showChar = 100;
	var ellipsestext = &quot;...&quot;;
	var moretext = &quot;more&quot;;
	var lesstext = &quot;less&quot;;
	$('.more').each(function() {
		var content = $(this).html();

		if(content.length &gt; showChar) {

			var c = content.substr(0, showChar);
			var h = content.substr(showChar-1, content.length - showChar);

			var html = c + '&lt;span class=&quot;moreellipses&quot;&gt;' + ellipsestext+ '&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;morecontent&quot;&gt;&lt;span&gt;' + h + '&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;a href=&quot;&quot; class=&quot;morelink&quot;&gt;' + moretext + '&lt;/a&gt;&lt;/span&gt;';

			$(this).html(html);
		}

	});

	$(&quot;.morelink&quot;).click(function(){
		if($(this).hasClass(&quot;less&quot;)) {
			$(this).removeClass(&quot;less&quot;);
			$(this).html(moretext);
		} else {
			$(this).addClass(&quot;less&quot;);
			$(this).html(lesstext);
		}
		$(this).parent().prev().toggle();
		$(this).prev().toggle();
		return false;
	});
});
</pre><h2>Online Demo</h2><p><a target="_blank" href="http://viralpatel.net/blogs/demo/jquery/show-more-link-shortened-content/">Click here to view demo</a><br /> <iframe src="http://viralpatel.net/blogs/demo/jquery/show-more-link-shortened-content/demo.html" width="100%" height="200px" ></iframe></p><h2>Download Source Code</h2><p><a href="http://viralpatel.net/blogs/demo/jquery/show-more-link-shortened-content/download.zip">Click here to download (ZIP, 2KB)</a></p><p><strong>Update:</strong><br /> The above code is modified into jQuery plugin. Thanks to Jay. Following is the code.</p><h2>jQuery Plugin</h2><h3>Code</h3><pre class="brush: jscript; title: ; notranslate">
jQuery.fn.shorten = function(settings) {
  var config = {
    showChars : 100,
    ellipsesText : &quot;...&quot;,
    moreText : &quot;more&quot;,
    lessText : &quot;less&quot;
  };

  if (settings) {
    $.extend(config, settings);
  }

  $('.morelink').live('click', function() {
    var $this = $(this);
    if ($this.hasClass('less')) {
      $this.removeClass('less');
      $this.html(config.moreText);
    } else {
      $this.addClass('less');
      $this.html(config.lessText);
    }
    $this.parent().prev().toggle();
    $this.prev().toggle();
    return false;
  });

  return this.each(function() {
    var $this = $(this);

    var content = $this.html();
    if (content.length &gt; config.showChars) {
      var c = content.substr(0, config.showChars);
      var h = content.substr(config.showChars , content.length - config.showChars);
      var html = c + '&lt;span class=&quot;moreellipses&quot;&gt;' + config.ellipsesText + '&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;morecontent&quot;&gt;&lt;span&gt;' + h + '&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;a href=&quot;javascript://nop/&quot; class=&quot;morelink&quot;&gt;' + config.moreText + '&lt;/a&gt;&lt;/span&gt;';
      $this.html(html);
      $(&quot;.morecontent span&quot;).hide();
    }
  });
}</pre><h3>Download</h3><p><a target="_blank" href="http://viralpatel.net/blogs/demo/jquery/jquery.shorten.1.0.js">Click to login jQuery Shorten plugin</a></p><h3>Usage</h3><p><strong>Step 1:</strong> Include the jQuery plugin in your HTML</p><pre class="brush: xml; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;
	src=&quot;http://viralpatel.net/blogs/demo/jquery/jquery.shorten.1.0.js&quot;&gt;&lt;/script&gt;
</pre><p><strong>Step 2:</strong> Add the code to shorten any DIV content. In below example we are shortening DIV with class &#8220;comment&#8221;.</p><pre class="brush: xml; title: ; notranslate">
&lt;div class=&quot;comment&quot;&gt;
	This is a long comment text.
	This is a long comment text.
	This is a long comment text.
	This is a long comment text. This is a long comment text.
&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	$(document).ready(function() {

		$(&quot;.comment&quot;).shorten();

	});
&lt;/script&gt;
</pre><p>You may want to pass the parameters to shorten() method and override the default ones.</p><pre class="brush: jscript; title: ; notranslate">
$(&quot;.comment&quot;).shorten({
	&quot;showChars&quot; : 200
});

$(&quot;.comment&quot;).shorten({
	&quot;showChars&quot; : 150,
	&quot;moreText&quot;	: &quot;See More&quot;,
});

$(&quot;.comment&quot;).shorten({
	&quot;showChars&quot; : 50,
	&quot;moreText&quot;	: &quot;See More&quot;,
	&quot;lessText&quot;	: &quot;Less&quot;,
});
</pre><div id="relatedpost"><h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html" title="jQuery: Get the Text of Element without Child Element">jQuery: Get the Text of Element without Child Element</a></li><li><a href="http://viralpatel.net/blogs/2010/11/multiple-checkbox-select-deselect-jquery-tutorial-example.html" title="Multiple Checkbox Select/Deselect using jQuery &#8211; Tutorial with Example">Multiple Checkbox Select/Deselect using jQuery &#8211; Tutorial with Example</a></li><li><a href="http://viralpatel.net/blogs/2010/01/jquery-resizable-draggable-resize-drag-tutorial-example.html" title="jQuery Resizable and Draggable Tutorial with Example">jQuery Resizable and Draggable Tutorial with Example</a></li><li><a href="http://viralpatel.net/blogs/2009/09/change-form-input-textbox-style-focus-jquery.html" title="Changing Form Input (Textbox) Style on Focus using jQuery">Changing Form Input (Textbox) Style on Focus using jQuery</a></li><li><a href="http://viralpatel.net/blogs/2009/01/fadein-fadeout-fade-out-effect-in-html-text-div-using-jquery.html" title="FadeIn FadeOut html text div effect using jQuery.">FadeIn FadeOut html text div effect using jQuery.</a></li><li><a href="http://viralpatel.net/blogs/2008/12/creating-orkut-style-status-update-div-textbox-using-jquery.html" title="Creating orkut style status update div-textbox using jQuery.">Creating orkut style status update div-textbox using jQuery.</a></li><li><a href="http://viralpatel.net/blogs/2008/11/textbox-with-background-image-using-css.html" title="Textbox with background image using CSS">Textbox with background image using CSS</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://viralpatel.net/blogs/2010/12/dynamically-shortened-text-show-more-link-jquery.html/feed</wfw:commentRss> <slash:comments>16</slash:comments> </item> <item><title>jQuery Ajax &#8211; Handling unauthenticated requests via Ajax</title><link>http://viralpatel.net/blogs/2010/12/jquery-ajax-handling-unauthenticated-request-ajax.html</link> <comments>http://viralpatel.net/blogs/2010/12/jquery-ajax-handling-unauthenticated-request-ajax.html#comments</comments> <pubDate>Fri, 24 Dec 2010 10:12:54 +0000</pubDate> <dc:creator>Viral Patel</dc:creator> <category><![CDATA[AJAX]]></category> <category><![CDATA[JavaScript]]></category> <category><![CDATA[JQuery]]></category> <category><![CDATA[authentication]]></category> <category><![CDATA[http error]]></category> <category><![CDATA[jquery ajax tutorial]]></category><guid isPermaLink="false">http://viralpatel.net/blogs/?p=2118</guid> <description><![CDATA[Since few days I am working on a small project where I have to deal with lot of Ajax requests. The whole UI is designed such that only appropriate parts are refreshed with required information through Ajax. The application needs user authentication before accessing any part of it. Although this is easy to achieve in [...]]]></description> <content:encoded><![CDATA[<p><img src="http://img.viralpatel.net/2010/12/security_lock.png" alt="" title="security_lock" width="208" height="153" class="alignright size-full wp-image-2119" />Since few days I am working on a small project where I have to deal with lot of Ajax requests. The whole UI is designed such that only appropriate parts are refreshed with required information through Ajax.</p><p>The application needs user authentication before accessing any part of it. Although this is easy to achieve in any MVC based framework, its a whole new thing when dealing with Ajax requests.</p><p>In a normal request/response based MVC application, if a user is not authenticated and tries to view a webpage, the request is redirected to the Login page. Almost all MVC framework comes with this functionality. When the request gets redirected to Login page, the Status Code 403 (Forbidden) is set in HTTP header. 403 Forbidden is a HTTP status code returned by a web server when a user agent requests a resource that the server does not allow them to.</p><p>But when using Ajax for getting the content, if the request is redirected to Login page, it is difficult to handle this scenario.</p><p>Here is a simplest solution that can be implemented in your Ajax based application. Just add this code in your Javascript file.</p><pre class="brush: jscript; title: ; notranslate">
    $(document).ready(
        function() {
            $(&quot;body&quot;).ajaxError(
                function(e,request) {
                    if (request.status == 403) {
                        window.location.reload();
                        //window.location.href = &quot;/myapp/login&quot;;
                    }
                }
            );
        }
    );
</pre><p>In above code, we have added a handler function to ajaxError which gets called by jQuery whenever there is an error in the HTTP Response. The function checks if the status code is 403, if it is then it simply tries to reload the URL. This generate a new request to the server which automatically gets redirected to the login page. The only disadvantage of reloading the page is that you generate a new HTTP request which server has to handle and redirect to Login page.</p><p>You may wish to directly redirect user to login page from client code itself. Check the Line XX where we are changing the windows location and redirecting user. I personally dont like this method as you have to hard code the path for login page in javascript. I prefer using the previous solution to reload the page.</p><p>I am sure there must be several way of implementing this in Javascript. Share yours if you think you have a better one <img src='http://viralpatel.net/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><div id="relatedpost"><h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://viralpatel.net/blogs/2009/04/jquery-ajax-tutorial-example-ajax-jquery-development.html" title="jQuery AJAX Tutorial, Example: Simplify Ajax development with jQuery">jQuery AJAX Tutorial, Example: Simplify Ajax development with jQuery</a></li><li><a href="http://viralpatel.net/blogs/2009/02/handling-session-timeout-server-errors-http-ajax-session-ajax-session-timeout-problem.html" title="Handling Session timeout &#038; other server http errors in AJAX">Handling Session timeout &#038; other server http errors in AJAX</a></li><li><a href="http://viralpatel.net/blogs/2008/12/ajax-post-method-example-using-javascript-jquery.html" title="AJAX Post Method example using Javascript &#038; jQuery">AJAX Post Method example using Javascript &#038; jQuery</a></li><li><a href="http://viralpatel.net/blogs/2012/01/scroll-fix-header-jquery-facebook.html" title="Facebook Style Scroll Fixed Header in JQuery">Facebook Style Scroll Fixed Header in JQuery</a></li><li><a href="http://viralpatel.net/blogs/2011/02/clearable-textbox-jquery.html" title="Create a Clearable TextBox with jQuery">Create a Clearable TextBox with jQuery</a></li><li><a href="http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html" title="jQuery: Get the Text of Element without Child Element">jQuery: Get the Text of Element without Child Element</a></li><li><a href="http://viralpatel.net/blogs/2011/01/first-play-framework-gae-siena-application-tutorial-example.html" title="Your first Play! &#8211; GAE &#8211; Siena application: Tutorial with Example">Your first Play! &#8211; GAE &#8211; Siena application: Tutorial with Example</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://viralpatel.net/blogs/2010/12/jquery-ajax-handling-unauthenticated-request-ajax.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Multiple Checkbox Select/Deselect using jQuery &#8211; Tutorial with Example</title><link>http://viralpatel.net/blogs/2010/11/multiple-checkbox-select-deselect-jquery-tutorial-example.html</link> <comments>http://viralpatel.net/blogs/2010/11/multiple-checkbox-select-deselect-jquery-tutorial-example.html#comments</comments> <pubDate>Thu, 11 Nov 2010 07:08:34 +0000</pubDate> <dc:creator>Viral Patel</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[JQuery]]></category> <category><![CDATA[How-To]]></category> <category><![CDATA[jquery tips tricks]]></category><guid isPermaLink="false">http://viralpatel.net/blogs/?p=2113</guid> <description><![CDATA[Almost all the user interfaces that I have created had this functionality of selecting multiple items from a list to process them or delete them. Although its very very easy to implement this functionality in Javascript, using jQuery for this is real fun. I will show you a simple implementation of adding multiple checkbox select [...]]]></description> <content:encoded><![CDATA[<p><img src="http://img.viralpatel.net/2010/11/multiple-checkbox-select-jqeury.jpg" alt="multiple-checkbox-select-jquery" title="multiple-checkbox-select-jquery" width="177" height="225" class="alignleft size-full wp-image-2114" />Almost all the user interfaces that I have created had this functionality of selecting multiple items from a list to process them or delete them. Although its very very easy to implement this functionality in Javascript, using jQuery for this is real fun. I will show you a simple implementation of adding multiple checkbox select and deselect functionality to any webpage. We will have a table with some data in it and checkbox in each row. There will be a select all checkbox in the header of the table. If user select/deselect the selectall checkbox, all the checkbox in table will get selected or deselected accordingly. Now one more thing we would like here to add is, suppose user select all the checkbox one by one then the selectall checkbox should be automatically gets selected. And if user click selectall first and then unchecks any of the checkbox, then the selectall also should be unchecked automatically.</p><h2>The HTML</h2><pre class="brush: xml; title: ; notranslate">
&lt;HTML&gt;
&lt;HEAD&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;
	&lt;TITLE&gt;Multiple Checkbox Select/Deselect - DEMO&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
	&lt;H2&gt;Multiple Checkbox Select/Deselect - DEMO&lt;/H2&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;tr&gt;
	&lt;th&gt;&lt;input type=&quot;checkbox&quot; id=&quot;selectall&quot;/&gt;&lt;/th&gt;
	&lt;th&gt;Cell phone&lt;/th&gt;
	&lt;th&gt;Rating&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td align=&quot;center&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;case&quot; name=&quot;case&quot; value=&quot;1&quot;/&gt;&lt;/td&gt;
	&lt;td&gt;BlackBerry Bold 9650&lt;/td&gt;
	&lt;td&gt;2/5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td align=&quot;center&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;case&quot; name=&quot;case&quot; value=&quot;2&quot;/&gt;&lt;/td&gt;
	&lt;td&gt;Samsung Galaxy&lt;/td&gt;
	&lt;td&gt;3.5/5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td align=&quot;center&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;case&quot; name=&quot;case&quot; value=&quot;3&quot;/&gt;&lt;/td&gt;
	&lt;td&gt;Droid X&lt;/td&gt;
	&lt;td&gt;4.5/5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td align=&quot;center&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;case&quot; name=&quot;case&quot; value=&quot;4&quot;/&gt;&lt;/td&gt;
	&lt;td&gt;HTC Desire&lt;/td&gt;
	&lt;td&gt;3/5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td align=&quot;center&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;case&quot; name=&quot;case&quot; value=&quot;5&quot;/&gt;&lt;/td&gt;
	&lt;td&gt;Apple iPhone 4&lt;/td&gt;
	&lt;td&gt;5/5&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;/BODY&gt;
&lt;/HTML&gt;
</pre><h2>The jQuery Code</h2><p>Add following jQuery code in HEAD section of above HTML</p><pre class="brush: jscript; title: ; notranslate">
&lt;SCRIPT language=&quot;javascript&quot;&gt;
$(function(){

	// add multiple select / deselect functionality
	$(&quot;#selectall&quot;).click(function () {
		  $('.case').attr('checked', this.checked);
	});

	// if all checkbox are selected, check the selectall checkbox
	// and viceversa
	$(&quot;.case&quot;).click(function(){

		if($(&quot;.case&quot;).length == $(&quot;.case:checked&quot;).length) {
			$(&quot;#selectall&quot;).attr(&quot;checked&quot;, &quot;checked&quot;);
		} else {
			$(&quot;#selectall&quot;).removeAttr(&quot;checked&quot;);
		}

	});
});
&lt;/SCRIPT&gt;
</pre><p>Here in above code at line 05, we register a handler on click of selectall checkbox. On click of this checkbox, we check/uncheck all checkbox in the datatable.<br /> Also check the link 13, where we check the selectall checkbox, if all the checkbox are selected manually. To get more idea, play with the below demo.</p><h2>Online Demo</h2><p><iframe src="http://viralpatel.net/blogs/demo/jquery/multiple-checkbox-select-deselect/demo-article.html" width="100%" height="180px"></iframe></p><p><a href="http://viralpatel.net/blogs/demo/jquery/multiple-checkbox-select-deselect/jquery-select-multiple-checkbox.html">Click here to view Online Demo</a></p><div id="relatedpost"><h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html" title="jQuery: Get the Text of Element without Child Element">jQuery: Get the Text of Element without Child Element</a></li><li><a href="http://viralpatel.net/blogs/2008/12/creating-orkut-style-status-update-div-textbox-using-jquery.html" title="Creating orkut style status update div-textbox using jQuery.">Creating orkut style status update div-textbox using jQuery.</a></li><li><a href="http://viralpatel.net/blogs/2011/02/clearable-textbox-jquery.html" title="Create a Clearable TextBox with jQuery">Create a Clearable TextBox with jQuery</a></li><li><a href="http://viralpatel.net/blogs/2010/12/dynamically-shortened-text-show-more-link-jquery.html" title="Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery">Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery</a></li><li><a href="http://viralpatel.net/blogs/2010/01/javascript-array-remove-element-js-array-delete-element.html" title="JavaScript Array Remove an Element ">JavaScript Array Remove an Element </a></li><li><a href="http://viralpatel.net/blogs/2010/01/jquery-resizable-draggable-resize-drag-tutorial-example.html" title="jQuery Resizable and Draggable Tutorial with Example">jQuery Resizable and Draggable Tutorial with Example</a></li><li><a href="http://viralpatel.net/blogs/2009/09/default-text-label-textbox-javascript-jquery.html" title="Default Text Label in Textbox using JavaScript/jQuery">Default Text Label in Textbox using JavaScript/jQuery</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://viralpatel.net/blogs/2010/11/multiple-checkbox-select-deselect-jquery-tutorial-example.html/feed</wfw:commentRss> <slash:comments>19</slash:comments> </item> <item><title>Understanding jQuery animate() function</title><link>http://viralpatel.net/blogs/2010/03/understanding-jquery-animate-function.html</link> <comments>http://viralpatel.net/blogs/2010/03/understanding-jquery-animate-function.html#comments</comments> <pubDate>Tue, 09 Mar 2010 12:38:15 +0000</pubDate> <dc:creator>Viral Patel</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[JQuery]]></category> <category><![CDATA[jQuery Effects]]></category><guid isPermaLink="false">http://viralpatel.net/blogs/?p=2045</guid> <description><![CDATA[jQuery animate() function is very powerful API to manipulate html elements and add animation functionality. The use of animate function is very simple. First lets check the syntax of this function. .animate( properties, [ duration ], [ easing ], [ callback ] ) properties: A map of CSS properties that the animation will move toward. [...]]]></description> <content:encoded><![CDATA[<p><img src="http://img.viralpatel.net/jquery-logo.png" alt="" title="jquery-logo" width="236" height="85" class="alignright size-full wp-image-1108" />jQuery animate() function is very powerful API to manipulate html elements and add animation functionality. The use of animate function is very simple. First lets check the syntax of this function.</p><p><strong>.animate( properties, [ duration ], [ easing ], [ callback ] )</strong></p><ul><li>properties: A map of CSS properties that the animation will move toward.</li><li>duration: A string or number determining how long the animation will run.</li><li>easing: A string indicating which easing function to use for the transition.</li><li>callback: A function to call once the animation is complete.</li></ul><p><strong>.animate( properties, options )</strong></p><ul><li>properties: A map of CSS properties that the animation will move toward.</li><li>options: A map of additional options to pass to the method. Supported keys:<ul><li>duration: A string or number determining how long the animation will run.</li><li>easing: A string indicating which easing function to use for the transition.</li><li>complete: A function to call once the animation is complete.</li><li>step: A function to be called after each step of the animation.</li><li>queue: A Boolean indicating whether to place the animation in the effects queue. If false, the animation will begin immediately.</li><li>specialEasing: A map of one or more of the CSS properties defined by the properties argument and their corresponding easing functions (added 1.4).</li></ul></li></ul><p>Lets learn the animate() function with set of examples.</p><p>First include the jQuery javascript library in your html file. Add following in your html &lt;HEAD&gt; tag:</p><pre class="brush: xml; title: ; notranslate">
&lt;SCRIPT type=&quot;text/javascript&quot;
		src=&quot;http://code.jquery.com/jquery-latest.js&quot;&gt;&lt;/SCRIPT&gt;
</pre><p>For all the demos, we will use a sample DIV tag for animating. Following is the div code and its stylesheet.</p><pre class="brush: xml; title: ; notranslate">
&lt;style type=&quot;text/css&quot;&gt;
#content {
	background-color:#ffaa00;
	width:300px;
	height:30px;
	padding:3px;
}
&lt;/style&gt;
&lt;input type=&quot;button&quot; id=&quot;animate&quot; value=&quot;Animate&quot;/&gt;
&lt;div id=&quot;content&quot;&gt;Animate Height&lt;/div&gt;
</pre><h2>Animate height/width</h2><p>Animating height and width in jQuery is very easy. Lets assume you have a DIV that you want to animate i.e. increase the height.</p><pre class="brush: jscript; title: ; notranslate">
$(&quot;#animate&quot;).click(function() {
	$(&quot;#content&quot;).animate(
			{&quot;height&quot;: &quot;80px&quot;},
			&quot;fast&quot;);
});
</pre><p><strong>Demo 1:</strong><br /> <iframe src="http://viralpatel.net/blogs/demo/jquery/animate-demo/animate-height.html" width="100%" height="150px" frameborder="0"></iframe></p><p>Also following will be the code to animate Width of the element.</p><pre class="brush: jscript; title: ; notranslate">
$(&quot;#animate&quot;).click(function() {
	$(&quot;#content&quot;).animate(
			{&quot;width&quot;: &quot;350px&quot;},
			&quot;fast&quot;);
});
</pre><p><strong>Demo 2:</strong><br /> <iframe src="http://viralpatel.net/blogs/demo/jquery/animate-demo/animate-width.html" width="100%" height="120px" frameborder="0"></iframe></p><h2>Animate opacity</h2><pre class="brush: jscript; title: ; notranslate">
$(&quot;#animate&quot;).click(function() {
	$(&quot;#content&quot;).animate(
			{&quot;opacity&quot;: &quot;0.15&quot;},
			&quot;slow&quot;);
});
</pre><p><strong>Demo 3:</strong><br /> <iframe src="http://viralpatel.net/blogs/demo/jquery/animate-demo/animate-opacity.html" width="100%" height="120px" frameborder="0"></iframe></p><h2>Moving elements using animate()</h2><pre class="brush: xml; title: ; notranslate">
&lt;STYLE&gt;
#content {
	background-color:#6688ff;
	position:absolute;
	width:100px;
	height:100px;
	padding:3px;
	margin-top:5px;
	left: 100px;
}
&lt;/STYLE&gt;
&lt;input type=&quot;button&quot; id=&quot;left&quot; value=&quot;Left&quot;/&gt;
&lt;input type=&quot;button&quot; id=&quot;right&quot; value=&quot;Right&quot;/&gt;
&lt;div id=&quot;content&quot;&gt;Move&lt;/div&gt;
</pre><pre class="brush: jscript; title: ; notranslate">
$(&quot;#right&quot;).click(function() {
	$(&quot;#content&quot;).animate(
			{&quot;left&quot;: &quot;+=50px&quot;},
			&quot;slow&quot;);
});
$(&quot;#left&quot;).click(function() {
	$(&quot;#content&quot;).animate(
			{&quot;left&quot;: &quot;-=50px&quot;},
			&quot;slow&quot;);
});
</pre><p><strong>Demo 4:</strong><br /> <iframe src="http://viralpatel.net/blogs/demo/jquery/animate-demo/animate-move.html" width="100%" height="150px" frameborder="0"></iframe></p><h2>Callback Function</h2><p>Callback functions are very useful to perform certain activity when the animation is completed. Also note here when multiple elements are mapped with the animation and we have specified a callback function. Then the callback will get called for each of the element.</p><p>Let us see an example where we use callback function to display a message when animation is completed.</p><pre class="brush: jscript; title: ; notranslate">
$(&quot;#animate&quot;).click(function() {
	$(&quot;#content&quot;).animate(
			{&quot;height&quot;: &quot;100px&quot;, &quot;width&quot;: &quot;250px&quot;},
			&quot;slow&quot;, function(){
				$(this).html(&quot;Animation Completed&quot;);
			});
});</pre><p><strong>Demo 5:</strong><br /> <iframe src="http://viralpatel.net/blogs/demo/jquery/animate-demo/animate-callback.html" width="100%" height="150px" frameborder="0"></iframe></p><h2>Combine multiple animations</h2><p>You may want to combine multiple animations. Following are few demos will help you understanding this.</p><p><strong>Example 1</strong>: Animate both height and width at same time.<br /> This example is pretty straight forward. You can animate both height and width at same time by specifying it in animate function. For example: In below code we specified height and width value in animate function.</p><pre class="brush: jscript; title: ; notranslate">
$(&quot;#animate&quot;).click(function() {
	$(&quot;#content&quot;).animate(
			{&quot;height&quot;: &quot;100px&quot;, &quot;width&quot;: &quot;250px&quot;},
			&quot;slow&quot;, );
});</pre><p><strong>Example 2</strong>: Queuing the animations.</p><pre class="brush: jscript; title: ; notranslate">
$(&quot;#animate&quot;).click(function() {
	$(&quot;#content&quot;)
		.animate({&quot;height&quot;: &quot;100px&quot;}, 500)
		.animate({&quot;width&quot;: &quot;250px&quot;}, 500);
});
</pre><p><strong>Demo 6:</strong><br /> <iframe src="http://viralpatel.net/blogs/demo/jquery/animate-demo/animate-queue.html" width="100%" height="150px" frameborder="0"></iframe></p><h2>Queuing of Events</h2><p>In above demo (Demo 6) we saw that when we queued up the events with multiple .animate() method call, the animation is actually queued up. i.e. it completes the first animation and then proceed with next. Let see an example were we use <code>queue</code> parameter to disable queuing. In following example we have set parameter <code>queue</code> to <code>false</code>. The code is exactly same as demo 6, only change we added is queue = false. Also note that queue parameter is added with second argument.</p><pre class="brush: jscript; title: ; notranslate">
$(&quot;#animate&quot;).click(function() {
	$(&quot;#content&quot;)
		.animate({&quot;height&quot;: &quot;100px&quot;}, {&quot;queue&quot;: false, &quot;duration&quot;: 500})
		.animate({&quot;width&quot;: &quot;250px&quot;}, 500);
});
</pre><p><strong>Demo 7:</strong><br /> <iframe src="http://viralpatel.net/blogs/demo/jquery/animate-demo/animate-queue-false.html" width="100%" height="150px" frameborder="0"></iframe></p><div id="relatedpost"><h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://viralpatel.net/blogs/2009/10/jquery-bounce-effect-bounce-html-js.html" title="Fantastic Bouncy Effect using jQuery/JavaScript">Fantastic Bouncy Effect using jQuery/JavaScript</a></li><li><a href="http://viralpatel.net/blogs/2009/09/create-accordion-menu-jquery.html" title="Create Simplest Accordion Menu using jQuery">Create Simplest Accordion Menu using jQuery</a></li><li><a href="http://viralpatel.net/blogs/2009/08/20-top-jquery-tips-tricks-for-jquery-programmers.html" title="20 Top jQuery Tips &#038; Tricks for jQuery Programmers">20 Top jQuery Tips &#038; Tricks for jQuery Programmers</a></li><li><a href="http://viralpatel.net/blogs/2009/07/delete-row-html-table-clicking-using-jquery.html" title="Delete Row from HTML Table by clicking it using jQuery">Delete Row from HTML Table by clicking it using jQuery</a></li><li><a href="http://viralpatel.net/blogs/2009/03/how-to-apply-html-user-interface-effects-using-jquery.html" title="How to apply HTML User Interface Effects using jQuery.">How to apply HTML User Interface Effects using jQuery.</a></li><li><a href="http://viralpatel.net/blogs/2012/01/scroll-fix-header-jquery-facebook.html" title="Facebook Style Scroll Fixed Header in JQuery">Facebook Style Scroll Fixed Header in JQuery</a></li><li><a href="http://viralpatel.net/blogs/2011/02/clearable-textbox-jquery.html" title="Create a Clearable TextBox with jQuery">Create a Clearable TextBox with jQuery</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://viralpatel.net/blogs/2010/03/understanding-jquery-animate-function.html/feed</wfw:commentRss> <slash:comments>15</slash:comments> </item> <item><title>oEmbed: An Open Format Every Developer Should Know About</title><link>http://viralpatel.net/blogs/2010/01/oembed-open-format-web-developers.html</link> <comments>http://viralpatel.net/blogs/2010/01/oembed-open-format-web-developers.html#comments</comments> <pubDate>Tue, 26 Jan 2010 09:24:36 +0000</pubDate> <dc:creator>Viral Patel</dc:creator> <category><![CDATA[JQuery]]></category> <category><![CDATA[Web 2.0]]></category> <category><![CDATA[oembed]]></category> <category><![CDATA[youtube]]></category><guid isPermaLink="false">http://viralpatel.net/blogs/?p=2000</guid> <description><![CDATA[Most of the internet users spend their online time in creating and sharing content on internet. Sharing has became one of the most popular activity of internet users. This is attributed to formats like RSS and services like Twitter, Facebook etc. that allows user to share content with each other. oEmbed addresses the important task [...]]]></description> <content:encoded><![CDATA[<p><img src="http://img.viralpatel.net/2010/01/oembed-all-service.png" alt="oembed-all-service" title="oembed-all-service" width="352" height="180" class="aligncenter size-full wp-image-2002" /><br /> Most of the internet users spend their online time in creating and sharing content on internet. Sharing has became one of the most popular activity of internet users. This is attributed to formats like RSS and services like Twitter, Facebook etc. that allows user to share content with each other.</p><p>oEmbed addresses the important task of sharing and embedding information anywhere on internet. It allows developers to build applications that can easily access data from vast number of websites like youtube, flickr, google video, wordpress, wikipedia and many many more.</p><p>So what&#8217;s oEmbed? Well if you go by the definition on oEmbed&#8217;s <a rel="nfollow" target="_blank" href="http://www.oembed.com/">official website</a>:</p><blockquote><p>oEmbed is a format for allowing an embedded representation of a URL on third party sites. The simple API allows a website to display embedded content (such as photos or videos) when a user posts a link to that resource, without having to parse the resource directly.</p></blockquote><p>The best example of oEmbed can be Facebook&#8217;s status update feature. When you paste a youtube movie link in Facebook&#8217;s status bar, it automatically identify the link content as youtube movie and embed it in the status. Similarly you can paste any kind of content link: Flickr, Wikipedia etc.</p><p><img src="http://img.viralpatel.net/2010/01/facebook-oembed.png" alt="facebook-oembed" title="facebook-oembed" width="564" height="364" class="aligncenter size-full wp-image-2001" /></p><h2>Basics of oEmbed</h2><p>In this document I will use two terms very often, <strong>consumer</strong> and <strong>provider</strong>. Consumer is the client who wants to convert a link into embedded content. And Provider is one who get the request and send the oembed response. For example, if you paste a youtube URL in Facebook&#8217;s status update, Facebook will be the consumer and Youtube will be the provider.</p><p>A consumer who wants to convert a link into content will make an HTTP request like:</p><pre class="brush: xml; title: ; notranslate">

http://www.flickr.com/services/oembed/?url=http%3A//www.flickr.com/photos/bees/2341623661/
</pre><p>and the provider, in this case Flickr will send response in JSON format:</p><pre class="brush: xml; title: ; notranslate">
{
	&quot;version&quot;: &quot;1.0&quot;,
	&quot;type&quot;: &quot;photo&quot;,
	&quot;width&quot;: 240,
	&quot;height&quot;: 160,
	&quot;title&quot;: &quot;ZB8T0193&quot;,
	&quot;url&quot;: &quot;http://farm4.static.flickr.com/3123/2341623661_7c99f48bbf_m.jpg&quot;,
	&quot;author_name&quot;: &quot;Bees&quot;,
	&quot;author_url&quot;: &quot;http://www.flickr.com/photos/bees/&quot;,
	&quot;provider_name&quot;: &quot;Flickr&quot;,
	&quot;provider_url&quot;: &quot;http://www.flickr.com/&quot;
}
</pre><p><strong>Youtube example:</strong><br /> Request URL:</p><pre class="brush: xml; title: ; notranslate">

http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3DM3r2XDceM6A&#038;format=json
</pre><p>Response:</p><pre class="brush: xml; title: ; notranslate">
{
	&quot;version&quot;: &quot;1.0&quot;,
	&quot;type&quot;: &quot;video&quot;,
	&quot;provider_name&quot;: &quot;YouTube&quot;,
	&quot;provider_url&quot;: &quot;http://youtube.com/&quot;,
	&quot;width&quot;: 425,
	&quot;height&quot;: 344,
	&quot;title&quot;: &quot;Amazing Nintendo Facts&quot;,
	&quot;author_name&quot;: &quot;ZackScott&quot;,
	&quot;author_url&quot;: &quot;http://www.youtube.com/user/ZackScott&quot;,
	&quot;html&quot;:
		&quot;&lt;object width=\&quot;425\&quot; height=\&quot;344\&quot;&gt;
			&lt;param name=\&quot;movie\&quot; value=\&quot;http://www.youtube.com/v/M3r2XDceM6A&amp;fs=1\&quot;&gt;&lt;/param&gt;
			&lt;param name=\&quot;allowFullScreen\&quot; value=\&quot;true\&quot;&gt;&lt;/param&gt;
			&lt;param name=\&quot;allowscriptaccess\&quot; value=\&quot;always\&quot;&gt;&lt;/param&gt;
			&lt;embed src=\&quot;http://www.youtube.com/v/M3r2XDceM6A&amp;fs=1\&quot;
				type=\&quot;application/x-shockwave-flash\&quot; width=\&quot;425\&quot; height=\&quot;344\&quot;
				allowscriptaccess=\&quot;always\&quot; allowfullscreen=\&quot;true\&quot;&gt;&lt;/embed&gt;
		&lt;/object&gt;&quot;,
}</pre><p>Once the consumer get the response, all it has to do is to utilize the content and embed it in the page.</p><h3>Request Format</h3><p>The consumer who is requesting for the embed content must follow some basic rules while sending request. Requests sent to the API must be HTTP GET requests, with all arguments sent as query parameters. All arguments must be urlencoded.<br /> Following are the request parameters:</p><ul><li><strong>url</strong> (required): This is the URL for which embedded content is requested</li><li><strong>maxwidth</strong> (optional): An optional parameter for maxwidth can be passed. Some of the content provider who support this param will send the response content accordingly. For example, Flickr may set the maxwidth of image.</li><li><strong>maxheight</strong> (optional): Similar to maxwidth, maxheight is an optional param.</li><li><strong>format</strong> (optional): By default the content provider&#8217;s response is in JSON. But consumer may want the response in other formats like XML. This parameter will determine is response format</li></ul><h3>Response Format</h3><p>The provider who sends the response back to consumer may send it with following parameters.</p><ul><li><strong>type</strong> (required): The resource type. Valid values, along with value-specific parameters, are described below.</li><li><strong>version</strong> (required): The oEmbed version number. This must be 1.0.</li><li><strong>title</strong> (optional): A text title, describing the resource.</li><li><strong>author_name</strong> (optional): The name of the author/owner of the resource.</li><li><strong>author_url</strong> (optional): A URL for the author/owner of the resource.</li><li><strong>provider_name</strong> (optional): The name of the resource provider.</li><li><strong>provider_url</strong> (optional): The url of the resource provider.</li><li><strong>cache_age</strong> (optional): The suggested cache lifetime for this resource, in seconds. Consumers may choose to use this value or not.</li><li><strong>thumbnail_url</strong> (optional): A URL to a thumbnail image representing the resource. The thumbnail must respect any maxwidth and maxheight parameters. If this paramater is present, thumbnail_width and thumbnail_height must also be present.</li><li><strong>thumbnail_width</strong> (optional): The width of the optional thumbnail. If this paramater is present, thumbnail_url and thumbnail_height must also be present.</li><li><strong>thumbnail_height</strong> (optional): The height of the optional thumbnail. If this paramater is present, thumbnail_url and thumbnail_width must also be present.</li></ul><p>The provider may wants to add more attributes to the response.</p><h2>Letting People Know You Support oEmbed</h2><p>The oEmbed providers can make their service discoverable by adding following element in HEAD for the HTML document.</p><pre class="brush: xml; title: ; notranslate">
&lt;link rel=&quot;alternate&quot; type=&quot;application/json+oembed&quot;
	href=&quot;http://flickr.com/services/oembed?url=http%3A//flickr.com/photos/bees/2362225867/&amp;format=json&quot;
	title=&quot;Bacon Lollys oEmbed Profile&quot; /&gt;
&lt;link rel=&quot;alternate&quot; type=&quot;text/xml+oembed&quot;
	href=&quot;http://flickr.com/services/oembed?url=http%3A//flickr.com/photos/bees/2362225867/&amp;format=xml&quot;
	title=&quot;Bacon Lollys oEmbed Profile&quot; /&gt;
</pre><p>The URLs contained within the href attribute should be the full oEmebed endpoint plus URL and any needed format parameter. No other request parameters should be included in this URL.</p><p>The type attribute must contain either application/json+oembed for JSON responses, or text/xml+oembed for XML.</p><h2>oEmbed Demo with jQuery</h2><p>Integrating oEmbed support in your application using jQuery is very easy using <a rel="nofollow" target="_blank" href="http://code.google.com/p/jquery-oembed/">jQuery oEmbed plugin</a>. Following is the source code the of demo.<br /> <b>The HTML Code</b></p><pre class="brush: xml; title: ; notranslate">
Content URL:
&lt;input type=&quot;text&quot; id=&quot;update&quot;/&gt;&lt;button id=&quot;btn&quot;&gt;Get&lt;/button&gt;
&lt;div id=&quot;embed&quot;&gt;&lt;/div&gt;
</pre><p><b>The jQuery Code</b></p><pre class="brush: jscript; title: ; notranslate">
$(function(){
	$('#btn').click(function() {
		$(&quot;#embed&quot;).oembed($('#update').val());
	});
});
</pre><p>Don&#8217;t forget to include jquery library and jquery oembed javascript in the demo.<br /> <b>The Demo</b><br /> Enter any oEmbed supporting URL (Youtube video link or Flickr link <strong>http://www.flickr.com/photos/stuckincustoms/2035748576/</strong>) in below textbox and press Get.<br /> <iframe src="http://viralpatel.net/blogs/demo/oembed/demo.html" width="100%" height="300px"></iframe><br /> (<a target="_blank" href="http://viralpatel.net/blogs/demo/oembed/demo.html">demo link</a>)</p><h3>oEmbed in PHP</h3><p>There is a small PHP library that can be used to integrate oEmbed support in PHP. <a rel="nofollow" target="_blank" href="http://code.google.com/p/php-oembed/">oEmbed-PHP</a> is a library that provides OEmbed compatible interface for javascript and PHP clients to consume. It can deal with OEmbed providers by acting as a proxy, and it has a plugin system where you can add your own embed-providers.<br /> Import this small PHP library and directly use it in your application.</p><pre class="brush: php; title: ; notranslate">
  $manager = ProviderManager::getInstance();
  $obj=$manager-&gt;provide(&quot;http://www.youtube.com/watch? v=EQqJSAOOmGI&quot;,&quot;object&quot;);
  $html=$obj-&gt;renderClass();
</pre><h2>oEmbed for other languages</h2><p><b>Perl</b><br /> Web-oEmbed (<a rel="nofollow" target="_blank" href="http://search.cpan.org/~miyagawa/Web-oEmbed/">http://search.cpan.org/~miyagawa/Web-oEmbed/</a>)</p><p><b>Ruby</b><br /> oembed_links (<a rel="nofollow" target="_blank" href="http://github.com/netshade/oembed_links">http://github.com/netshade/oembed_links</a>)</p><p><b>Python</b><br /> Python oEmbed (<a rel="nofollow" target="_blank" href="http://code.google.com/p/python-oembed/">http://code.google.com/p/python-oembed/</a>)</p><div id="relatedpost"><h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://viralpatel.net/blogs/2010/10/introduction-html5-domstorage-api-example.html" title="Introduction to HTML5 DOMStorage API with Example">Introduction to HTML5 DOMStorage API with Example</a></li><li><a href="http://viralpatel.net/blogs/2010/06/facebook-facts-you-didnt-know.html" title="Facebook: Facts you probably didnt know">Facebook: Facts you probably didnt know</a></li><li><a href="http://viralpatel.net/blogs/2009/09/gartners-hype-cycle-special-report-2009.html" title=" Gartner&#8217;s Hype Cycle Special Report for 2009"> Gartner&#8217;s Hype Cycle Special Report for 2009</a></li><li><a href="http://viralpatel.net/blogs/2009/07/youtube-experimenting-3d-videos.html" title="YouTube Experimenting with 3D videos!">YouTube Experimenting with 3D videos!</a></li><li><a href="http://viralpatel.net/blogs/2009/05/gravatar-manage-your-user-avatars-for-free.html" title="Gravatar: Manage your user avatars for free">Gravatar: Manage your user avatars for free</a></li><li><a href="http://viralpatel.net/blogs/2009/05/web-30-is-around-the-corner.html" title="Web 3.0 is around the corner !">Web 3.0 is around the corner !</a></li><li><a href="http://viralpatel.net/blogs/2009/05/capture-website-screenshot-screenshot-capture-utilities.html" title="Capture Website Screenshot with screenshot capture utilities">Capture Website Screenshot with screenshot capture utilities</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://viralpatel.net/blogs/2010/01/oembed-open-format-web-developers.html/feed</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>jQuery Resizable and Draggable Tutorial with Example</title><link>http://viralpatel.net/blogs/2010/01/jquery-resizable-draggable-resize-drag-tutorial-example.html</link> <comments>http://viralpatel.net/blogs/2010/01/jquery-resizable-draggable-resize-drag-tutorial-example.html#comments</comments> <pubDate>Mon, 25 Jan 2010 09:18:15 +0000</pubDate> <dc:creator>Viral Patel</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[JQuery]]></category> <category><![CDATA[How-To]]></category> <category><![CDATA[jquery drag drop]]></category> <category><![CDATA[jquery resize]]></category> <category><![CDATA[jquery resizeable]]></category><guid isPermaLink="false">http://viralpatel.net/blogs/?p=2006</guid> <description><![CDATA[Have you ever tried doing some animation using plain Javascript or moving DIVs here and there or resizing them?! Well, you know then how much pain it is as not only you have to tackle the difficult part of animation but also making it cross browser compatible. Well you probably know why I am stretching [...]]]></description> <content:encoded><![CDATA[<p><img src="http://img.viralpatel.net/2009/05/jquery-thumb.jpg" alt="" title="jquery-thumb" width="250" height="178" class="alignright size-full wp-image-1244" />Have you ever tried doing some animation using plain Javascript or moving DIVs here and there or resizing them?! Well, you know then how much pain it is as not only you have to tackle the difficult part of animation but also making it cross browser compatible.</p><p>Well you probably know why I am stretching up so hard is just to tell you how easy it is to use jQuery and do some really fantastic things without even bothering of knowing how it is done internally. jQuery comes with really useful UI library that can be incorporated to do such tricks.</p><p>In this small note, I will show you how to do Resizing and Dragging of a DIV using jQuery UI. I will show you the demo of a DIV, which can be resized by pulling up the edges and also dragged here and there.</p><h2>Step 1: Setting up jQuery UI and CSS</h2><p>We need to install jQuery UI library first in order to do resizing/dragging. Add following lines in your HEAD of html document.</p><pre class="brush: xml; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;
	src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;
	src=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot;
	href=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css&quot;/&gt;
</pre><p>Note that we have imported jquery and jquery-ui library. Also, to use Resizable we must import jquery-ui.css file. This stylesheet will be used by resizable to add resize corner and other internal styles.</p><h2>Step 2: Write jQuery to Resize and Drag</h2><p>For the demo we will define a small DIV which we will make resizable and draggable.</p><pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;resizeDiv&quot;&gt;&lt;/div&gt;
</pre><p>Now just add following jQuery code in your document.</p><pre class="brush: jscript; title: ; notranslate">
$('#resizeDiv')
	.draggable()
	.resizable();
</pre><p>The beauty of jQuery is that we can simply chain the function calling. First we called .draggable() function that makes the DIV draggable and then we called resizable().<br /> You may want to define callback functions on start/stop/resize events. To do so we simply define:</p><pre class="brush: jscript; title: ; notranslate">
$('#resizeDiv')
	.resizable({
		start: function(e, ui) {
			alert('resizing started');
		},
		resize: function(e, ui) {

		},
		stop: function(e, ui) {
			alert('resizing stopped');
		}
	});
</pre><p>Note that the callback function gets two arguments. First is <strong>event</strong> object and next is <strong>ui</strong>.<br /> The ui object has the following fields:</p><ul><li><strong>ui.helper</strong> &#8211; a jQuery object containing the helper element</li><li><strong>ui.originalPosition</strong> &#8211; {top, left} before resizing started</li><li><strong>ui.originalSize</strong> &#8211; {width, height} before resizing started</li><li><strong>ui.position</strong> &#8211; {top, left} current position</li><li><strong>ui.size</strong> &#8211; {width, height} current size</li></ul><p>For more information about jQuery Draggable (Drag and Drop) read this article.<br /> <a target="_blank" href="http://viralpatel.net/blogs/2009/05/implement-drag-and-drop-example-jquery-javascript-html.html">jQuery Draggable (Drag and Drop)</a></p><h2>Online Demo</h2><p>Following is the online demo of jQuery Resizable and Draggable.<br /> <iframe src="http://viralpatel.net/blogs/demo/jquery-resize-drag/demo.html" width="100%" height="300px"></iframe><br /> (<a target="_blank" href="http://viralpatel.net/blogs/demo/jquery-resize-drag/"><strong>Demo Link</strong></a>)</p><div id="relatedpost"><h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html" title="jQuery: Get the Text of Element without Child Element">jQuery: Get the Text of Element without Child Element</a></li><li><a href="http://viralpatel.net/blogs/2010/12/dynamically-shortened-text-show-more-link-jquery.html" title="Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery">Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery</a></li><li><a href="http://viralpatel.net/blogs/2010/11/multiple-checkbox-select-deselect-jquery-tutorial-example.html" title="Multiple Checkbox Select/Deselect using jQuery &#8211; Tutorial with Example">Multiple Checkbox Select/Deselect using jQuery &#8211; Tutorial with Example</a></li><li><a href="http://viralpatel.net/blogs/2009/05/implement-drag-and-drop-example-jquery-javascript-html.html" title="Drag and Drop Example using jQuery JavaScript in HTML">Drag and Drop Example using jQuery JavaScript in HTML</a></li><li><a href="http://viralpatel.net/blogs/2009/01/fadein-fadeout-fade-out-effect-in-html-text-div-using-jquery.html" title="FadeIn FadeOut html text div effect using jQuery.">FadeIn FadeOut html text div effect using jQuery.</a></li><li><a href="http://viralpatel.net/blogs/2008/12/creating-orkut-style-status-update-div-textbox-using-jquery.html" title="Creating orkut style status update div-textbox using jQuery.">Creating orkut style status update div-textbox using jQuery.</a></li><li><a href="http://viralpatel.net/blogs/2012/01/scroll-fix-header-jquery-facebook.html" title="Facebook Style Scroll Fixed Header in JQuery">Facebook Style Scroll Fixed Header in JQuery</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://viralpatel.net/blogs/2010/01/jquery-resizable-draggable-resize-drag-tutorial-example.html/feed</wfw:commentRss> <slash:comments>13</slash:comments> </item> <item><title>jQuery Live Event</title><link>http://viralpatel.net/blogs/2009/12/jquery-live-events.html</link> <comments>http://viralpatel.net/blogs/2009/12/jquery-live-events.html#comments</comments> <pubDate>Mon, 14 Dec 2009 08:55:31 +0000</pubDate> <dc:creator>Viral Patel</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[JQuery]]></category> <category><![CDATA[JQuery Events]]></category><guid isPermaLink="false">http://viralpatel.net/blogs/?p=1922</guid> <description><![CDATA[I love jQuery. I have expressed my love for jQuery here, here and here and still I feel so much for it. Also check out the 20 jQuery Tips &#038; Tricks Tutorial. Let us see a wonderful function available in jQuery which many of us don&#8217;t know. Most common use of jQuery is to attach [...]]]></description> <content:encoded><![CDATA[<p><img src="http://img.viralpatel.net/2009/12/jquery-live-function.png" alt="jquery-live-function" title="jquery-live-function" width="257" height="175" class="alignleft size-full wp-image-1923" />I love jQuery. I have expressed my love for jQuery <a href="http://viralpatel.net/blogs/2009/06/textarea-resize-javascript-jquery-plugin-resize-textarea-html.html">here</a>, <a href="http://viralpatel.net/blogs/2009/09/change-form-input-textbox-style-focus-jquery.html">here</a> and <a href="http://viralpatel.net/blogs/2009/04/jquery-ajax-tutorial-example-ajax-jquery-development.html">here</a> and still I feel so much for it. Also check out the <a href="http://viralpatel.net/blogs/2009/08/20-top-jquery-tips-tricks-for-jquery-programmers.html">20 jQuery Tips &#038; Tricks</a> Tutorial.</p><p>Let us see a wonderful function available in jQuery which many of us don&#8217;t know. Most common use of jQuery is to attach an event some callback function to any element on webpage.</p><p>When we attach any event to element on webpage using normal functions such as mouseclick or click or mouseover, it attach the event only to current elements. If dynamically we add another element in future then this handler is not attached to those dynamic elements.</p><p>For example, we have a button on click of which we display an alert message. We can use <code>$('#buttonid').click()</code> to attach a handler function which can display alert message. But what if we dynamically add another button on page and we want exactly the same functionality? Well, jQuery Live Function comes to rescue for such requirements.</p><p>Let us see an example of normal event handling and one with Live function. In this example, we are dynamically adding list items that has hover effect. When mouse is hover on this list item, it changes the color. We will do this by both traditional way and using Live function.</p><h2>The HTML</h2><p>Following is the HTML Code for our example:</p><pre class="brush: xml; title: ; notranslate">
&lt;TABLE&gt;
&lt;TR valign=&quot;top&quot;&gt;
	&lt;td&gt;
	&lt;input type=&quot;button&quot; value=&quot;Click To Add (without Live)&quot; id=&quot;add&quot;/&gt;
	&lt;ul id=&quot;list&quot; class=&quot;list&quot;&gt;
		&lt;li&gt;&amp;nbsp;&lt;/li&gt;
		&lt;li&gt;&amp;nbsp;&lt;/li&gt;
		&lt;li&gt;&amp;nbsp;&lt;/li&gt;
		&lt;li&gt;&amp;nbsp;&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/td&gt;
	&lt;td&gt;
	&lt;input type=&quot;button&quot; value=&quot;Click To Add (with Live)&quot; id=&quot;addLive&quot;/&gt;
	&lt;ul id=&quot;listLive&quot; class=&quot;list&quot;&gt;
		&lt;li&gt;&amp;nbsp;&lt;/li&gt;
		&lt;li&gt;&amp;nbsp;&lt;/li&gt;
		&lt;li&gt;&amp;nbsp;&lt;/li&gt;
		&lt;li&gt;&amp;nbsp;&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/td&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
</pre><p>The HTML will display two buttons and two list sets. One button will dynamically add list item to one list using normal click event, where as another button will use Live events to do this.</p><h2>The CSS</h2><p>We will use following CSS in our example:</p><pre class="brush: css; title: ; notranslate">
.list	li 	{
	list-style: none;
	background-color:#eeffaa;
	width:150px;
	margin-top:5px;
	border:1px solid #8AAC00;
}

.list li.selected {
	background-color: #ffaaee;
}
</pre><h2>The jQuery</h2><p>First we will attach an event with two buttons to dynamically add a list item.</p><pre class="brush: jscript; title: ; notranslate">
$('#add').click(function(){
	$('#list').append('&lt;li&gt;&amp;nbsp;&lt;/li&gt;');
});
$('#addLive').click(function(){
	$('#listLive').append('&lt;li&gt;&amp;nbsp;&lt;/li&gt;');
});
</pre><p>Then, we will make the first list hoverable using mouseover and mouseout event.</p><pre class="brush: jscript; title: ; notranslate">
$('#list&gt;li')
		.mouseover(function(){ $(this).addClass('selected'); })
		.mouseout(function(){ $(this).removeClass('selected'); });
</pre><p>And, then we will make the second list hoverable using <code>live()</code> event.</p><pre class="brush: jscript; title: ; notranslate">
$('#listLive&gt;li')
	.live('mouseover', function(){ $(this).addClass('selected');   })
	.live('mouseout',  function(){ $(this).removeClass('selected'); });
</pre><p>Notice the difference in above code. In first snippet we are using mouseover and mouseout functions to attach events with handlers to change the background color of list item. When we add a list item dynamically, this handler are not attached to newly created list item. And in second snippet we are using live() to attach event mouseover and mouseout.</p><p>This way we ensure that any dynamically added element will have the events handler attached with them.<br /> <b>jQuery 1.3 and below</b><br /> Currently jQuery 1.3 and below is not supporting events blur, focus, mouseenter, mouseleave, change, submit.<br /> But the good news is that jQuery 1.4 alpha 1 has just <a rel="nofollow" target="_new" href="http://blog.jquery.com/2009/12/04/jquery-14-alpha-1-released/">been released</a> which supports these events.</p><h2>Important Points</h2><p>Following are few important points one should note before working with Live events.</p><ul><li>When you bind a &#8220;live&#8221; event it will bind to all current and future elements on the page.</li><li>Live events do not bubble in the traditional manner and cannot be stopped using <code>stopPropagation</code> or <code>stopImmediatePropagation</code>.</li><li>Live events currently only work when used against a selector. For example, this would work: <code>$("li a").live(...)</code> but this would not: <code>$("a", someElement).live(...)</code> and neither would this: <code>$("a").parent().live(...)</code>.</li><li>.live doesn&#8217;t support the no-event style callback that liveQuery provides. Only event handlers can be bound with .live.</li><li>.live doesn&#8217;t have a &#8220;setup&#8221; or &#8220;cleanup&#8221; step, since all events are delegated rather than bound directly to an element.</li><li>To remove a live event you should use the <code>die()</code> method</li></ul><h2>Live Demo</h2><p>Following is the live demo for jQuery Live Event Example.<br /> <iframe src="http://viralpatel.net/blogs/demo/jquery-live-event-function/demo.html" height="300px" width="100%"></iframe><br /> <a target="_blank" href="http://viralpatel.net/blogs/demo/jquery-live-event-function/"><strong>Click To View Demo</strong></a><br /> <a href="http://viralpatel.net/blogs/demo/jquery-live-event-function/source.zip"><strong>Download Source Code</strong></a></p><div id="relatedpost"><h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://viralpatel.net/blogs/2012/01/scroll-fix-header-jquery-facebook.html" title="Facebook Style Scroll Fixed Header in JQuery">Facebook Style Scroll Fixed Header in JQuery</a></li><li><a href="http://viralpatel.net/blogs/2009/04/tutorial-handle-browser-events-using-jquery-javascript-framework.html" title="Tutorial: Handle browser events using jQuery JavaScript framework">Tutorial: Handle browser events using jQuery JavaScript framework</a></li><li><a href="http://viralpatel.net/blogs/2011/02/clearable-textbox-jquery.html" title="Create a Clearable TextBox with jQuery">Create a Clearable TextBox with jQuery</a></li><li><a href="http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html" title="jQuery: Get the Text of Element without Child Element">jQuery: Get the Text of Element without Child Element</a></li><li><a href="http://viralpatel.net/blogs/2011/01/first-play-framework-gae-siena-application-tutorial-example.html" title="Your first Play! &#8211; GAE &#8211; Siena application: Tutorial with Example">Your first Play! &#8211; GAE &#8211; Siena application: Tutorial with Example</a></li><li><a href="http://viralpatel.net/blogs/2010/12/dynamically-shortened-text-show-more-link-jquery.html" title="Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery">Dynamically shortened Text with &#8220;Show More&#8221; link using jQuery</a></li><li><a href="http://viralpatel.net/blogs/2010/12/jquery-ajax-handling-unauthenticated-request-ajax.html" title="jQuery Ajax &#8211; Handling unauthenticated requests via Ajax">jQuery Ajax &#8211; Handling unauthenticated requests via Ajax</a></li></ul></div>]]></content:encoded> <wfw:commentRss>http://viralpatel.net/blogs/2009/12/jquery-live-events.html/feed</wfw:commentRss> <slash:comments>5</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: viralpatel.net @ 2012-02-07 10:37:59 -->
