<?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/"
xmlns:series="http://unfoldingneurons.com/"
><channel><title>Larry Ullman &#187; ajax</title> <atom:link href="http://www.larryullman.com/tag/ajax/feed/" rel="self" type="application/rss+xml" /><link>http://www.larryullman.com</link> <description>Translating Geek Into English</description> <lastBuildDate>Mon, 21 May 2012 11:03:07 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>What Is Larry Thinking? #54 =&gt; Salt, Pepper, 99designs, and Ajax</title><link>http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/</link> <comments>http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/#comments</comments> <pubDate>Mon, 30 Apr 2012 11:00:06 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Ruby]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[99designs]]></category> <category><![CDATA[ajax]]></category> <category><![CDATA[newsletter]]></category> <category><![CDATA[phpvqp3]]></category> <category><![CDATA[security]]></category> <category><![CDATA[travel]]></category> <category><![CDATA[yii]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=3190</guid> <description><![CDATA[In this edition… About This Newsletter On the Web? =&#62; Properly Salting Passwords, The Case Against Pepper On the Road =&#62; Istanbul and Silicon Valley On the Blog =&#62; My New Logo and Business Card from 99designs On the Blog =&#62; Five Ways to Lose Work On the Blog =&#62; Yii and Me (aka, the [...]]]></description> <content:encoded><![CDATA[<p> In this edition…</p><ul><li><a
href="http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/#about">About This Newsletter</a></li><li><a
href="http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/#web">On the Web? =&gt; Properly Salting Passwords, The Case Against Pepper</a></li><li><a
href="http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/#road">On the Road =&gt; Istanbul and Silicon Valley</a></li><li><a
href="http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/#blog1">On the Blog =&gt; My New Logo and Business Card from 99designs</a></li><li><a
href="http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/#blog2">On the Blog =&gt; Five Ways to Lose Work</a></li><li><a
href="http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/#blog3">On the Blog =&gt; Yii and Me (aka, the Yii Book)</a></li><li><a
href="http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/#qa1">Q&amp;A =&gt; How Do I Make Ajax Content Available to Search Engines</a></li><li><a
href="http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/#qa2">Q&amp;A =&gt; Can You Use .html Instead of .php?</a></li><li><a
href="http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/#news">Larry Ullman&#8217;s Book News => &#8220;PHP 5 Advanced: Visual QuickPro Guide&#8221; (3rd Edition)</a></li></ul><p> <span
id="more-3190"></span></p><h2 id="about">About This Newsletter</h2><p>To repeat what I&#8217;ve said in the past two newsletters, I am now officially on <a
href="http://twitter.com">Twitter</a>. Hopefully next month, I&#8217;ll get the Twitter &#8220;follow me&#8221; link on my main site and add it to this newsletter template. But in the meantime, you can follow me using <a
href="https://twitter.com/#!/LarryUllman">@LarryUllman</a>. I&#8217;m doing better about sending out tweets, and I do retweet things that I think are notable or useful. In fact, I sent out a tweet a couple of days ago asking for good newsletter questions, and I received a few, one of which I answer here. Even an old dog can learn a new trick, I guess.</p><p>Also, I would love to have more questions to answer in forthcoming newsletters. I tend to get the most questions when I do a book giveaway, and I haven&#8217;t had one of those in a while, so the well is running a bit dry.</p><p>As always, questions, comments, and all feedback are much appreciated. And thanks for your interest in what I have to say and do!</p><h2 id="web">On the Web => Properly Salting Passwords, The Case Against Pepper</h2><p>Anthony Ferrara, creator of the PHP <a
href="https://github.com/ircmaxell/PHP-PasswordLib">PasswordLib</a> library,&nbsp;just recently <a
href="http://blog.ircmaxell.com/2012/04/properly-salting-passwords-case-against.html">posted a discussion of using salts and pepper</a> to improve the security of a stored password. Mostly, the article is a discussion of why a pepper is unnecessary (and if you don&#8217;t know what a &#8220;pepper&#8221; is, just read the article), but the posting also does a good job of explaining the purpose of a salt, and why the salt does not need to be a secret. The posting is relatively short, and is something I think everyone can benefit from reading.</p><p>The posting ends with the most important security fact:</p><blockquote><p>Remember, the most dangerous kind of security is a false sense of it. Thinking you&#8217;ve made your application more secure, when in fact you&#8217;ve weakened it, is the worst thing you could possibly do.</p></blockquote><h2 id="road">On the Road => Istanbul and Silicon Valley</h2><p>For the first time in a long time, I&#8217;m going to do some work-related traveling. I used to travel somewhat regularly for conferences and training seminars and the like. But then I had kids and other personal issues came up, so I cut back. I <em>think</em> the last time I travelled for work was when I spoke at a Voices That Matter conference in Nashville, TN in 2008!</p><p>My first trip is to Istanbul, Turkey, at the end of May. I&#8217;ll be speaking at the <a
href="http://e-commerceexpo.com/">E-commerce Expo</a> on May 30th (the site is in Turkish, so you&#8217;ll want to use a browser like Chrome that will translate the page for you). I will have about two other days to see what I can of the city, so if anyone has any recommendations of what to see and do, or where to eat, please let me know. I&#8217;ve heard nothing but great things about Istanbul and am really looking forward to it. Oh, and my speech—<strong>Building a Successful E-commerce Venture, or Failing Gracefully</strong>—should be good, too.</p><p>At the end of June, I&#8217;ll be doing two days of training on JavaScript and jQuery at the Mid-Pacific ICT Center Summer 2012 Faculty Development Week in Fremont, California. I&#8217;m tentatively thinking about making myself available for drinks or dinner on Thursday, June 28th, although I haven&#8217;t thought it through, yet. If you&#8217;re in the area and think you&#8217;d like to meet, let me know and I&#8217;ll see what makes sense from there. Thanks!</p><h2 id="blog1">On the Blog => My New Logo and Business Card from 99designs</h2><p>In March, I finally got around to having a new logo and business card created, using <a
href="http://99designs.com">99designs</a>. It wasn&#8217;t a driving need for me, but it was about time. I wrote about <a
href="http://www.larryullman.com/2012/04/18/my-99designs-logo-and-business-card-contest/">the contest experience</a> in one blog post. In another blog post, I show <a
href="http://www.larryullman.com/2012/04/16/my-new-logo-and-business-card/">the end results</a>, and also some of the terrible logos and business cards I&#8217;ve had along the way (i.e., the ones I designed).</p><p>I&#8217;ve since hired a designer to modify my CSS to use the new logo and colors. This is going to go with a new WordPress theme, which I&#8217;m hoping to get online in May. Hoping. After that I&#8217;ll also update this newsletter template.</p><h2 id="blog2">On the Blog => Five Ways to Lose Work</h2><p>In <a
href="http://www.larryullman.com/2012/04/02/what-is-larry-thinking-53-writing-and-working/#thinking">the previous newsletter</a>, I wrote about competing for work. Getting work is a frequent topic of conversation I have with readers, and between that question, and my recent experiences with 99designs, I wrote a blog post titled &#8220;<a
href="http://www.larryullman.com/2012/04/11/five-ways-to-lose-work/">Five Ways to Lose Work</a>&#8221;. Although some of the examples in that post come from my 99designs experience, it&#8217;s not really about 99designs, but more about the common mistakes people make when trying to get a project. My intent is that by recognizing these mistakes, you&#8217;ll be less likely to make them, and therefore have a better chance of getting work.</p><h2 id="blog3">On the Blog => Yii and Me (aka, the Yii Book)</h2><p>Many people have appreciated my <a
href="http://www.larryullman.com/series/learning-the-yii-framework/">Learning the Yii Framework</a> series and are awaiting a formal Yii book written by me. This is something I have been hoping to do for sometime. It looks like that time will be the latter half of 2012. Almost definitely. In <a
href="http://www.larryullman.com/2012/04/23/yii-and-me-aka-the-yii-book/">a recent blog post</a>, I announced my current plans and thinking about the book. I was also very pleased to say that Qiang Xue, the creator of Yii, has generously agreed to act as the technical editor for the book. Now it&#8217;s just a matter of writing it!</p><h2 id="qa1">Q&amp;A => How Do I Make Ajax Content Available to Search Engines?</h2><p>I&#8217;m digging way through the archives to find questions to answer, and I came across this one sent in by Richard back in October of 2008. It&#8217;s probably way too late to help Richard, but perhaps my answer can be of use to others. Here&#8217;s the prompt (summarized by me):</p><blockquote><p>I have a discussion board that pulls posts via Ajax, which means that posts aren&#8217;t being indexed by search engines. I updated the system so that all posts are displayed by PHP, to make them search engine visible, and then use Ajax for new posts since the user got online, but are there other places where Ajax presents this sort of problem?</p></blockquote><p>This brings up a very good point that every Web developer ought to be aware of. It&#8217;s obvious that if someone disables JavaScript, they can&#8217;t see the JavaScript functionality on a site. Statistically, only around 1-3% of all <em>users</em> have JavaScript disabled. However, <en>all search engine bots</en> are unable to see JavaScript-derived content. This means that all content on your site that&#8217;s created by JavaScript will not appear in search engines. This is especially ironic as many sites use JavaScript for the most important content. So what&#8217;s the solution?</p><p>The solution is actually the same solution for all JavaScript-based sites. Unless the site absolutely has to require JavaScript, you should always start with a non-JavaScript version first. A non-JavaScript version will be accessible by all search engines and by, well, anyone. The non-JavaScript version may not be pretty or cool, but it will be functional, which is most important. Then you can add the JavaScript layer to implement the cool, or more interactive, version. This process is called &#8220;progressive enhancement&#8221; and is a cornerstone of modern JavaScript, as explained in my &#8220;<a
href="http://www.larryullman.com/books/modern-javascript-develop-and-design/">Modern JavaScript: Develop and Design</a>&#8221; book.</p><p>If, for whatever reason, you don&#8217;t want to take this approach, then simply create alternative versions of your content that would be available for search engines. This could be as simple as a site map that links to non-dynamic versions of the content.</p><h2 id="qa2">Q&amp;A => Can You Use .html Instead of .php?</h2><p>This question came in from @enxaneta_info via Twitter:</p><blockquote><p>Is it possible to replace the <strong>.php</strong> extension with <strong>.html</strong>?</p></blockquote><p>The short answer is yes. A file&#8217;s extension just tells the computer what application should be used to execute that file. With Web files, the extension tells the Web server how to treat that file. Normally, the <strong>.php</strong> extension indicates that the code therein should be run through the PHP module. If you wanted to use <strong>.html</strong>, you&#8217;d just need to tell the Web server to send all files with a <strong>.html</strong> extension through the PHP module. (This is done via an AddType directive in the Apache configuration file.)</p><p>One benefit of doing this is that it allows you to hide what type of server-side tool is being used. On the other hand, there is certain to be a performance hit as the Web server will send every page through the PHP module. But if you have a site that only contains PHP scripts and has no HTML files, this is a reasonable change to make.</p><h2 id="news">Larry Ullman&#8217;s Book News => &#8220;PHP 5 Advanced: Visual QuickPro Guide&#8221; (3rd Edition)</h2><p>I&#8217;ve formally started writing the third edition of my &#8220;<a
href="http://www.larryullman.com/books/php-5-advanced-visual-quickpro-guide-2nd-edition/">PHP 5 Advanced: Visual QuickPro Guide</a>&#8221; book. You can view the initial <a
href="http://www.larryullman.com/2012/04/09/php-5-advanced-visual-quickpro-guide-3rd-edition-table-of-contents/">Table of Contents</a> on my Web site, along with some discussion of my approach to this edition. The new edition is tentatively titled &#8220;Advanced PHP and Object-Oriented Programming: Visual QuickPro Guide&#8221;, to better reflect the book&#8217;s focus.</p><p>If you have any thoughts on the book&#8217;s table of contents, please share them on that blog posting page. As I said, I&#8217;m writing the book now, and it will be my primary focus for the next two months. Ideally the first submitted draft will be completed by the end of May (although that will be a stretch), and the book will come out by the end of summer.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2012/04/30/what-is-larry-thinking-54-salt-pepper-99designs/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Examples from &#8220;Building a Web Site with Ajax: Visual QuickProject&#8221;</title><link>http://www.larryullman.com/2011/12/27/examples-from-building-a-web-site-with-ajax-visual-quickproject/</link> <comments>http://www.larryullman.com/2011/12/27/examples-from-building-a-web-site-with-ajax-visual-quickproject/#comments</comments> <pubDate>Tue, 27 Dec 2011 22:18:56 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[ajax]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2936</guid> <description><![CDATA[Although I don&#8217;t normally do this, per a reader request, I&#8217;ve made the book&#8217;s examples available to be seen and used here. The book has three primary examples, each in an Ajax and non-Ajax form. The pages are: dept_form.html This page returns all of the employees in a selected department. add_employee.html This page uses Ajax [...]]]></description> <content:encoded><![CDATA[<p>Although I don&#8217;t normally do this, per a reader request, I&#8217;ve made the book&#8217;s examples available to be seen and used here. The book has three primary examples, each in an Ajax and non-Ajax form. The pages are:</p><dl><dt><a
href="http://demo.larryullman.com/ajax/examples/dept_form.html">dept_form.html</a></dt><dd>This page returns all of the employees in a selected department.</dd><dt><a
href="http://demo.larryullman.com/ajax/examples/add_employee.html">add_employee.html</a></dt><dd>This page uses Ajax to add an employee to the database (note: the version running on this site does not actually execute the INSERT query that updates the database.)</dd><dt><a
href="http://demo.larryullman.com/ajax/examples/search_form.html">search_form.html</a></dt><dd>This page provides a simple search to retrieve employees by last name.</dd></dl><p>There&#8217;s no navigation within or among the pages. You&#8217;ll need to click Back to return to this page to see another example. To run one of the examples without Ajax (to see what that&#8217;d be like for visitors that can&#8217;t use the Ajax version), disable JavaScript in your Web browser.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/12/27/examples-from-building-a-web-site-with-ajax-visual-quickproject/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>“Ajax-ifying a Shopping Cart” Article Published Online</title><link>http://www.larryullman.com/2011/03/11/%e2%80%9cajax-ifying-a-shopping-cart%e2%80%9d-article-published-online/</link> <comments>http://www.larryullman.com/2011/03/11/%e2%80%9cajax-ifying-a-shopping-cart%e2%80%9d-article-published-online/#comments</comments> <pubDate>Fri, 11 Mar 2011 02:48:24 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[ajax]]></category> <category><![CDATA[article]]></category> <category><![CDATA[e-commerce]]></category> <category><![CDATA[ecom]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=2375</guid> <description><![CDATA[Peachpit Press has just published online another article I wrote titled “Ajax-ifying a Shopping Cart.” The article can be used to expand some of the content in my “Effortless E-Commerce with PHP and MySQL” book. This is the fourth and final article I wrote for Peachpit Press in support of this book (plus the five [...]]]></description> <content:encoded><![CDATA[<p>Peachpit Press has just published online another article I wrote titled “<a
href="http://www.peachpit.com/articles/article.aspx?p=1684314">Ajax-ifying a Shopping Cart</a>.” The article can be used to expand some of the content in my “Effortless E-Commerce with PHP and MySQL” book. This is the fourth and final article I wrote for Peachpit Press in support of this book (plus the five blog postings).</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2011/03/11/%e2%80%9cajax-ifying-a-shopping-cart%e2%80%9d-article-published-online/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Ajaxload</title><link>http://www.larryullman.com/2009/05/14/ajaxload/</link> <comments>http://www.larryullman.com/2009/05/14/ajaxload/#comments</comments> <pubDate>Thu, 14 May 2009 13:25:32 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[ajax]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=414</guid> <description><![CDATA[I StumbledUpon a notable Ajax resource recently, called Ajaxload. This site provides tools for generating an animated GIF that can be used on your site to indicate that something is loading or happening or whatever. Choose a style you like, the background color, the foreground color, and the transparency of the background, then click &#8220;Generate [...]]]></description> <content:encoded><![CDATA[<p>I <a
href="http://www.stumbleupon.com/">StumbledUpon</a> a notable Ajax resource recently, called <a
href="http://www.ajaxload.info/">Ajaxload</a>. This site provides tools for generating an animated GIF that can be used on your site to indicate that something is loading or happening or whatever. Choose a style you like, the background color, the foreground color, and the transparency of the background, then click &#8220;Generate it!&#8221; and you have a nice, custom graphic. The site&#8217;s in beta now and is free; I&#8217;m not sure if it&#8217;ll remain so or not (hopefully it will).</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2009/05/14/ajaxload/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>jQuery Tips, Tricks, and Miscellaneous</title><link>http://www.larryullman.com/2009/04/18/jquery-tips-tricks-and-miscellaneous/</link> <comments>http://www.larryullman.com/2009/04/18/jquery-tips-tricks-and-miscellaneous/#comments</comments> <pubDate>Sat, 18 Apr 2009 15:00:23 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[ajax]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[jquery]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=296</guid> <description><![CDATA[In this, the last post in my series on jQuery, I mention an odd lot of interesting things about jQuery that are worth knowing. Some involve specific code; others aid understanding of jQuery&#8217;s philosophy. You may or may not want to also read these earlier posts on jQuery: I think I noted this in an [...]]]></description> <content:encoded><![CDATA[<p>In this, the last post in my series on <a
href="http://www.jquery.com">jQuery</a>, I mention an odd lot of interesting things about jQuery that are worth knowing. Some involve specific code; others aid understanding of jQuery&#8217;s philosophy. You may or may not want to also read these earlier posts on jQuery:</p><ol><li> <a
href="http://www.larryullman.com/2009/02/05/a-quick-intro-to-jquery/">Incorporating jQuery into an HTML page</a></li><li> <a
href="http://www.larryullman.com/2009/02/07/a-quick-intro-to-jquery-part-2/">Selecting page elements</a></li><li> <a
href="http://www.larryullman.com/2009/03/02/a-quick-intro-to-jquery-part-3/">Adding event handlers</a></li><li> <a
href="http://www.larryullman.com/2009/03/05/a-quick-intro-to-jquery-part-4/">Applying effects</a></li><li> <a
href="http://www.larryullman.com/2009/03/15/a-quick-intro-to-jquery-part-5/">Manipulating elements</a></li><li> <a
href="http://www.larryullman.com/2009/03/29/a-quick-intro-to-jquery-part-6/">Ajax</a></li><li><a
href="http://www.larryullman.com/2009/04/11/a-quick-intro-to-jquery-part-7/">Plug-ins</a></li></ol><p><span
id="more-296"></span>I think I noted this in an earlier post, but an interesting feature of jQuery is that it ignores attempted actions that cannot be done. For example, if you attempt to hide an already hidden element, nothing happens. Specifically no error is thrown. While full-fledged programming in any language mandates that you avoid and watch for errors, jQuery is intended for quick and easy use, so this design means you don&#8217;t, in this example, need to test an element&#8217;s visibility before you attempt to hide it, greatly minimizing the amount of code you need to write.</p><p>Similarly, if you attempt to select something and no elements match the criteria, no objects are returned. Attempting to apply methods to a non-selection still doesn&#8217;t throw an error:</p><pre class="brush: jscript; title: ; notranslate">$('#thisDoesNotExist').addClass('varmit');</pre><p>If you want to confirm that a selection was made, you can apply the <strong>length()</strong> method to test for that:</p><pre class="brush: jscript; title: ; notranslate">var set = $('p');
if (set.length()) {...</pre><p>That code also shows how you can assign the results of the jQuery function call to another variable, making that result set available elsewhere in your code.</p><p>Going along the testing lines, for when you need to, you can see if an element has a particular class by using the <strong>is()</strong> method:</p><pre class="brush: jscript; title: ; notranslate">if ($('#x').is('.className') {...</pre><p>This method tests an expression and returns a Boolean value. Note the period before the CSS classname in this particular use of it.</p><p>To see if something is hidden, use the custom <em>:hidden</em> criteria:</p><pre class="brush: jscript; title: ; notranslate">if ($('#x').is(':hidden')) {...</pre><p>The <em>:visible</em> keyword is its corollary.</p><p>A variation on this is if you only want to select hidden or visible elements in the first place: <strong>$(&#8216;p:visible&#8217;)</strong> selects every visible paragraph.</p><p>Moving on randomly&#8230;a common need for a form is to disable the submit button after the user has clicked it, thereby preventing multiple submissions. To do so using jQuery, use <strong>$(&#8216;#submitButtonId&#8217;).attr(&#8216;disabled&#8217;, &#8216;disabled&#8217;)</strong>. That code selects the element with an ID value of <em>submitButtonId</em>, then applies the <em>disabled</em> attribute to it (in full XHTML compliance, the corresponding code would be <strong>disabled=&#8221;disabled&#8221;</strong>). You would obviously need to execute this code in a function that&#8217;s called upon the form&#8217;s submission (through an event handler).</p><p>Still quite randomly&#8230;in the first post I discussed how this code—</p><pre class="brush: jscript; title: ; notranslate">$(function() {
    // Do whatever.
});</pre><p>—is used to identify code to be executed once the document is ready (somewhat similar to the <em>onload</em> event). Interestingly, jQuery allows you to define <a
href="http://docs.jquery.com/Tutorials:Multiple_%24(document).ready()">many of these document ready functions</a>. This means you could put separate pieces of code into separate document ready functions or, more commonly, multiple included JavaScript files could each make use of this approach without overlapping or conflicting with each other.</p><p>Moving on more randomly and in conclusion&#8230;Firefox users, which I hope is everyone that develops Web pages, may also appreciate the <a
href="http://www.kintek.com.au/web-design-blog/jquery-menu-firefox-extension/">jQuery Plugin for Firefox</a>. It creates a menu that lets you more quickly access jQuery documentation. Speaking of which, when you&#8217;re looking at the jQuery manual, you may click on a link under API Reference to find methods within a specific subcategory, like <em>Effects</em>. The resulting page will list the methods in that category, with a brief description of each and its signature (i.e., what arguments it takes and what it returns). If you click on a method&#8217;s name on this page, you&#8217;ll be taken to another page that has a demo, with visible source code, for how that method can be used.</p><p>So that&#8217;s it! For now anyway. I&#8217;ll probably have more to say about jQuery in time. I do hope that you&#8217;ve found these posts to be useful. Of course, your best reference is the excellent yet managable <a
href="http://docs.jquery.com">jQuery documentation</a>. There are also plenty of articles in the documentation and linked from it. As always, thanks for reading and let me know if you have any comments or questions.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2009/04/18/jquery-tips-tricks-and-miscellaneous/feed/</wfw:commentRss> <slash:comments>11</slash:comments> <series:name><![CDATA[Introduction to jQuery]]></series:name> </item> <item><title>A Primer on jQuery Plug-Ins</title><link>http://www.larryullman.com/2009/04/11/a-quick-intro-to-jquery-part-7/</link> <comments>http://www.larryullman.com/2009/04/11/a-quick-intro-to-jquery-part-7/#comments</comments> <pubDate>Sat, 11 Apr 2009 17:25:23 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[ajax]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[jquery]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=312</guid> <description><![CDATA[In this, Part 7 of my (consistently delayed) series on the jQuery JavaScript framework, I talk about plug-ins you can use to expand jQuery&#8217;s features. You may want to read some of the previous six parts before getting into this one: jQuery itself is a relatively light framework that provides basic, cross-platform functionality. It was [...]]]></description> <content:encoded><![CDATA[<p>In this, Part 7 of my (consistently delayed) series on the <a
href="http://www.jquery.com">jQuery</a> JavaScript framework, I talk about <a
href="http://plugins.jquery.com/">plug-ins</a> you can use to expand jQuery&#8217;s features. You may want to read some of the previous six parts before getting into this one:</p><ol><li> <a
href="http://www.larryullman.com/2009/02/05/a-quick-intro-to-jquery/">Incorporating jQuery into an HTML page</a></li><li> <a
href="http://www.larryullman.com/2009/02/07/a-quick-intro-to-jquery-part-2/">Selecting page elements</a></li><li> <a
href="http://www.larryullman.com/2009/03/02/a-quick-intro-to-jquery-part-3/">Adding event handlers</a></li><li> <a
href="http://www.larryullman.com/2009/03/05/a-quick-intro-to-jquery-part-4/">Applying effects</a></li><li> <a
href="http://www.larryullman.com/2009/03/15/a-quick-intro-to-jquery-part-5/">Manipulating elements</a></li><li> <a
href="http://www.larryullman.com/2009/03/29/a-quick-intro-to-jquery-part-6/">Ajax</a></li></ol><p><span
id="more-312"></span></p><p>jQuery itself is a relatively light framework that provides basic, cross-platform functionality. It was intended to be easy to expand and build upon. This way, if you need X feature, it can be added, but if you don&#8217;t need that feature, you&#8217;re not forcing the user to download all the JavaScript for it. This is a great design, the only problem being how to find and use the plug-in you need.</p><p>To start, a lot of common functionality has been put into a separate library, called <a
href="http://jqueryui.com/">jQuery UI</a>, short for <em>User Interface</em>. This library adds features like drag and drop, resizing, accordians, tabs, date pickers, progress bars, plus effects. Even though jQuery UI contains all that stuff, you can use the <a
href="http://jqueryui.com/download">library&#8217;s download page</a> to build your own custom version that only defines the features you need. See the <a
href="http://jqueryui.com/demos/">jQuery UI demos page</a> for examples and source code. The <a
href="http://jqueryui.com/docs/Getting_Started">Getting Started</a> page also includes some good, basic information. In time, you&#8217;ll likely want to check out the <a
href="http://jqueryui.com/themeroller/">jQuery UI ThemeRoller</a>, which helps create a custom, cohesive look for the effects to match the rest of your site.</p><p>If jQuery UI is missing a feature that you need, head to <a
href="http://plugins.jquery.com/">jQuery&#8217;s plug-ins page</a>. There the plug-ins are organized by type, and there are listings of <a
href="http://plugins.jquery.com/most_popular">most popular</a> and <a
href="http://plugins.jquery.com/latest_releases">most recent releases</a>. If you want someone else&#8217;s opinion as to which plug-ins are best, a quick search turns up many articles on the subject:</p><ul><li><a
href="http://www.noupe.com/ajax/37-more-shocking-jquery-plugins.html">http://www.noupe.com/ajax/37-more-shocking-jquery-plugins.html</a></li><li><a
href="http://speckyboy.com/2008/07/21/20-amazing-jquery-plugins-and-65-excellent-jquery-resources/">http://speckyboy.com/2008/07/21/20-amazing-jquery-plugins-and-65-excellent-jquery-resources/</a></li><li><a
href="http://www.noupe.com/ajax/45-fresh-out-of-the-oven-jquery-plugins.html">http://www.noupe.com/ajax/45-fresh-out-of-the-oven-jquery-plugins.html</a></li><li><a
href="http://www.wiliam.com.au/wiliam-blog/the-best-web-2-jquery-javascript-plugins">http://www.wiliam.com.au/wiliam-blog/the-best-web-2-jquery-javascript-plugins</a></li><li><a
href="http://www.ajaxline.com/10-best-jquery-plugins-march-2009">http://www.ajaxline.com/10-best-jquery-plugins-march-2009</a></li></ul><p>There are a couple of plug-ins that I personally recommend: <a
href="http://users.tpg.com.au/j_birch/plugins/superfish/">Superfish</a> and the<a
href="http://www.malsup.com/jquery/form/"> jQuery Form Plugin</a>. The former creates &#8220;suckerfish&#8221;-style navigation menus out of nested unordered links. These are animated, keyboard accessible menus that degrade well. See the <a
href="http://users.tpg.com.au/j_birch/plugins/superfish/#examples">examples page</a> to look at this in action. The jQuery Form Plugin makes it easy to integrate Ajax functionality with forms (as form data is commonly used in Ajax interactions). If you look at the <a
href="http://www.malsup.com/jquery/form/#code-samples">examples</a>, you&#8217;ll see that it could hardly be easier to make an Ajax request using form data. Plus it has a cool feature where it automatically uses a hidden iframe to handle file uploads (which cannot be handled via Ajax).</p><p>So, what else? Date pickers are a popular tool and the jQuery UI includes a version. If you want something fancier, consider the <a
href="http://www.filamentgroup.com/lab/date_range_picker_using_jquery_ui_16_and_jquery_ui_css_framework/" class="broken_link">Date Range Picker</a> plug-in. There are also nice color pickers available, <a
href="http://www.eyecon.ro/colorpicker/">like this one</a>. And there are a ton of different image manipulation plug-ins out there, as you can see in the links above and on jQuery&#8217;s plug-in page.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2009/04/11/a-quick-intro-to-jquery-part-7/feed/</wfw:commentRss> <slash:comments>2</slash:comments> <series:name><![CDATA[Introduction to jQuery]]></series:name> </item> <item><title>Adding Ajax with jQuery</title><link>http://www.larryullman.com/2009/03/29/a-quick-intro-to-jquery-part-6/</link> <comments>http://www.larryullman.com/2009/03/29/a-quick-intro-to-jquery-part-6/#comments</comments> <pubDate>Sun, 29 Mar 2009 15:50:57 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[ajax]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[jquery]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=302</guid> <description><![CDATA[In this, the somewhat delayed Part 6 of my series on the jQuery JavaScript framework, I talk about performing Ajax requests using jQuery. You may want to read the previous five parts before getting into this one: Ajax, which may or may not stand for Asynchronous JavaScript and XML, depending upon whom you ask, is [...]]]></description> <content:encoded><![CDATA[<p>In this, the somewhat delayed Part 6 of my series on the <a
href="http://www.jquery.com">jQuery</a> JavaScript framework, I talk about performing Ajax requests using jQuery. You may want to read the previous five parts before getting into this one:</p><ol><li> <a
href="http://www.larryullman.com/2009/02/05/a-quick-intro-to-jquery/">Incorporating jQuery into an HTML page</a></li><li> <a
href="http://www.larryullman.com/2009/02/07/a-quick-intro-to-jquery-part-2/">Selecting page elements</a></li><li> <a
href="http://www.larryullman.com/2009/03/02/a-quick-intro-to-jquery-part-3/">Adding event handlers</a></li><li> <a
href="http://www.larryullman.com/2009/03/05/a-quick-intro-to-jquery-part-4/">Applying effects</a></li><li> <a
href="http://www.larryullman.com/2009/03/15/a-quick-intro-to-jquery-part-5/">Manipulating elements</a></li></ol><p><span
id="more-302"></span></p><p>Ajax, which may or may not stand for <em>Asynchronous JavaScript and XML</em>, depending upon whom you ask, is largely responsible for JavaScript&#8217;s current status. What was once a &#8220;toy&#8221; language used for rollover images at best, and annoying distractions at worst, is now a viable tool for developing Rich Internet Applications. The ability to make server-side requests behind-the-scenes so that user never has to leave the current page has transformed Web applications into more responsive, dynamic experiences, much more similar to what the user&#8217;s come to expect from their desktop applications (the expansion and adoption of broadband access is also a factor, among others). But this post isn&#8217;t intended to convince you to use Ajax, rather to show you how easily you can use it with jQuery. Because everything&#8217;s easier with jQuery!</p><p>There are a couple of different ways you can make Ajax requests using jQuery. To start, there&#8217;s the <strong>load()</strong> method. This method makes a request of a specific server-side resource and uses the returned result as the content for the element to which it&#8217;s applied. For example, say you have a DIV with an ID of <em>news</em>. You could load the current news, returned by a server-side script, into that DIV by doing this: <strong>$(&#8216;#news&#8217;).load(&#8216;fetch_news.php&#8217;)</strong>. In this example, the PHP script presumably just returns a bunch of text, which is then used as the <strong>innerHTML</strong> value for the selected element. You can pass GET values to the server-side script by appending them to the URL, or pass POST values by providing a second argument containing the data to be posted. If you check out the <a
href="http://docs.jquery.com/Ajax/load">jQuery documentation for load()</a>, you&#8217;ll see an example where part of the returned text is parsed out and used.</p><p>If you want to do something more dynamic with the returned response, you can use the aptly named <strong>get()</strong> and <strong>post()</strong> methods.Unlike a lot of the methods I&#8217;ve discussed in my posts thus far, these methods aren&#8217;t applied to specific elements. Instead, the syntax you&#8217;ll see are <strong>$.get()</strong> and <strong>$.post()</strong>. Any method that gets called like <strong>$.<em>methodName</em>()</strong> are utility methods, as they apply to the document as a whole, and not to any subset of the DOM.</p><p>The <strong>get()</strong> method takes up to four arguments: a URL, optional parameters (data to be sent to the server), a function to be called when the request is completed, and the type of response to expect. For this fourth argument, use a string: <em>text</em>, <em>html</em>, <em>xml</em>, <em>json</em>, <em>script</em>, or <em>jsonp</em>. As an example, say you want to call the server-side script to see if a username is available:</p><pre class="brush: jscript; title: ; notranslate">$.get('check_username.php', { username: 'test' },
    function(data) {
        alert (data);
    },
'text');</pre><p>So that makes a request to <strong>check_username.php</strong>, found in the same directory on the server as this current page. The request sends along a value of <em>test</em> assigned to <em>username</em> (so the PHP script would refer to <strong>$_GET['username']</strong>). The anonymous function will be called when the request is completed. It receives one argument, the returned response, assigned to <strong>data</strong>, which is then alerted. Finally, we state that text is expected back. That <strong>{ username: &#8216;test&#8217; }</strong> structure may seem a little strange. This is just one way of creating an object in JavaScript. In this case, it&#8217;s an anonymous object with one property (<em>username</em>). To pass multiple values, you would separate each by a comma: <strong>{ username: &#8216;test&#8217;, color: &#8216;blue&#8217;, timezone: &#8216;EST&#8217; }</strong>. Do note that the properties themselves are not quoted but their values are. Also, the anonymous function can be written to take two arguments, the second being a text version of the returned status, like <em>success</em> or <em>notmodified</em>.</p><p>In context, where you might check a provided username after the corresponding form field has changed, you would do this:</p><pre class="brush: jscript; title: ; notranslate"> // Call this anonymous function when the input's value changes:
$('#usernameInput').change(function() {

    // Create a new, empty object.
    var data = new Object();

    // Add a property called 'username' with a value of the form input's value:
    data.username = $('#usernameInput').val();

    $.get('check_username.php', data, function(response) { alert (response); }, 'text');

});</pre><p>(In a more complete example, though, you would confirm that the input has some value before making the request, and then probably do some error reporting based upon the result. Like if the result was a simple binary value for available/unavailable, you could add a message to the DOM or change the class of the form input&#8217;s label to indicate the unavailability of the username.)</p><p>The <strong>post()</strong> method is used exactly like <strong>get()</strong>, but makes a POST request. Generally speaking, you should make GET requests when you&#8217;re just fetching data from a page; you use POST when the intention is to alter content on the server. For comparison, normal viewing of a page in the browser, or clicking on a link, is a GET request; submitting a form is often a POST request, where the result of that request is something dynamic happening, like updating a record in a database or sending an email.</p><p>The primary Ajax-related jQuery method is&#8230;<strong>ajax()</strong>! Like <strong>get()</strong> and <strong>post()</strong>, <strong>ajax()</strong> is not invoked on a selected element. This method only takes one argument, which is an object of options. You can find the <a
href="http://docs.jquery.com/Ajax/jQuery.ajax#options">full list of options in jQuery&#8217;s manual</a>, but the key ones are: <strong>data</strong>, <strong>dataType</strong>, <strong>success</strong>, <strong>type</strong>, and <strong>url</strong>. The <strong>data</strong> is the data to be sent, as an object (like in the above <strong>get()</strong> example). The <strong>dataType</strong> option is the same as the fourth argument to <strong>get()</strong> and <strong>post()</strong>, with a value like <em>text</em>, <em>html</em>, <em>json</em>, <em>jsonp</em>, <em>script</em>, or <em>xml</em>. The value assigned to <strong>success</strong> is the function to be called when the request succeeds. The <strong>type</strong> is either <em>get</em> or <em>post</em>. The <strong>url</strong> value is the server-side script to which the request should be made.</p><p>Fleshing out the earlier <strong>get()</strong> example using <strong>ajax()</strong>, you&#8217;d have something like this:</p><pre class="brush: jscript; title: ; notranslate">// Call this anonymous function when the input's value changes:
$('#usernameInput').change(function() {

    // Create the data to be sent along as an object:
    var data = new Object();
    data.username = $('#usernameInput').val();

    // Create the Ajax options as an object:
    var options = new Object();
    options.data = data;
    options.dataType = 'text';
    options.type = 'get';
    options.success = function(response) {
        if (response == 1) { // Good!
            // Add a message:
            $('#usernameInputMsg').text('The username is available.');

            // Remove the error class, if present:
            $('#usernameInputLabel, #usernameInputMsg').removeClass('error');

        } else if (response == 0) { // Bad!
            // Add a message:
            $('#usernameInputMsg').text('The username is unavailable!');

            // Add the error class to two elements:
            $('#usernameInputLabel, #usernameInputMsg').addClass('error'); //
        }
    };
    options.url = 'check_username.php';

    $.ajax(options); // Make the request!

});</pre><p><a
href="http://www.larryullman.com/wp-content/2009/3/jquery3.html">You can see this in action here.</a> Note that for creating the both objects used in the request, I&#8217;m doing a longer, more formal object creation, to make it more clear what&#8217;s going on. There are more terse ways of accomplishing the same thing.<a
href="http://www.larryullman.com/wp-content/2009/3/jquery3.html"><br
/> </a></p><p>And that&#8217;s it for Ajax in jQuery. As always, there&#8217;s more information in the jQuery manual and in various tutorials online. And in my next post, I&#8217;ll discuss plug-ins, among them is the Form plug-in, which provides an easy way to submit form data via Ajax.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2009/03/29/a-quick-intro-to-jquery-part-6/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <series:name><![CDATA[Introduction to jQuery]]></series:name> </item> <item><title>DOM Manipulation using jQuery</title><link>http://www.larryullman.com/2009/03/15/a-quick-intro-to-jquery-part-5/</link> <comments>http://www.larryullman.com/2009/03/15/a-quick-intro-to-jquery-part-5/#comments</comments> <pubDate>Sun, 15 Mar 2009 13:35:24 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[ajax]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[jquery]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=300</guid> <description><![CDATA[If you&#8217;ve been reading my jQuery series of posts sequentially, then you saw , learned so that you can use or manipulate them, grasped , and saw that can easily be applied. In this post, I discuss other ways you can manipulate document elements: by altering their CSS, changing their values and attributes, and by [...]]]></description> <content:encoded><![CDATA[<p>If you&#8217;ve been reading my <a
href="http://www.jquery.com">jQuery</a> series of posts sequentially, then you saw <a
href="http://www.larryullman.com/2009/02/05/a-quick-intro-to-jquery/">how to incorporate jQuery into an HTML page</a>, learned <a
href="http://www.larryullman.com/2009/02/07/a-quick-intro-to-jquery-part-2/">how to select elements</a> so that you can use or manipulate them, grasped <a
href="http://www.larryullman.com/2009/03/02/a-quick-intro-to-jquery-part-3/">how to add event handlers</a>, and saw <a
href="http://www.larryullman.com/2009/03/05/a-quick-intro-to-jquery-part-4/">some of the effects</a> that can easily be applied. In this post, I discuss other ways you can manipulate document elements: by altering their CSS, changing their values and attributes, and by adding or removing elements. Although these techniques have an affect on the visual page, I&#8217;ve grouped them together here (separate from the effects post) as they&#8217;re all primarily about delivering content.<span
id="more-300"></span>I&#8217;ve already discussed the <strong>addClass()</strong> and <strong>removeClass()</strong> methods a couple of times now. The former adds a CSS class to an element; the latter removes it. You can also use the <strong>css()</strong> method to get an element&#8217;s property: <strong>$(&#8216;#something&#8217;).css(&#8216;font-weight&#8217;)</strong>. Or you can use the <strong>css()</strong> method to add specific style rules: <strong>$(&#8216;p&#8217;).css(&#8216;color&#8217;, &#8216;#ccc&#8217;)</strong>.</p><p>You can alter the size of an element using <strong>height()</strong> and <strong>width()</strong>. If called with an argument, that value will be the element&#8217;s new height or width accordingly. If either method is called without an argument, the element&#8217;s height or width is returned.</p><p>For more on the above methods, see <a
href="http://docs.jquery.com/CSS">jQuery&#8217;s documentation for CSS</a>.</p><p>A few very important methods I have yet to discuss come under <a
href="http://docs.jquery.com/Attributes">jQuery&#8217;s attributes</a> category.These are <strong>html()</strong>, <strong>text()</strong>, and <strong>val()</strong>. Each of these can retrieve or set an element&#8217;s HTML, text, or value. For example, <strong>html()</strong> will return the <strong>innerHTML</strong> value of an element, or if provided with an argument, will update that element&#8217;s <strong>innerHTML</strong>. The <strong>text()</strong> method is used the same way for an element&#8217;s <strong>text</strong> value. In other words, <strong>html()</strong> can get and set HTML; <strong>text()</strong> can only get and set text. The <strong>val()</strong> method gets or sets the value of an element, like form elements.</p><p>Say you have two form inputs—<em>firstName</em> and <em>lastName</em>. You can combine what the user enters there and place it in another element, like an paragraph greeting:</p><pre class="brush: jscript; title: ; notranslate">var name = $('#firstName').val() + ' ' + $('#lastName').val();
$('#greeting').html('Hello, &amp;lt;span class=&quot;bold&quot;&amp;gt;' + name + '&amp;lt;/span&amp;gt;');</pre><p>Then there&#8217;s the <strong>attr()</strong> method, short for <em>attribute</em>, can be used to retrieve or apply an attribute to an element. To get a specific link&#8217;s URL, you would use <strong>$(&#8216;#somelink&#8217;).attr(&#8216;href&#8217;)</strong>. To change every image so that it doesn&#8217;t have a border, that&#8217;d be <strong>$(&#8216;img&#8217;).attr(&#8216;border&#8217;, &#8217;0&#8242;)</strong>. Or, as a very useful example, to disable a submit button after a form submission (to prevent duplicate submissions), use: <strong>$(&#8216;#submit&#8217;).attr(&#8216;disabled&#8217;, &#8216;disabled&#8217;)</strong>.</p><p>The last topic to discuss in this post is DOM <a
href="http://docs.jquery.com/Manipulation">manipulation</a> through adding, moving, or removing elements. The <strong>append()</strong> method adds content to the end of an element; <strong>prepend()</strong> adds content to the beginning of it. Note that both methods add the content &#8220;inside&#8221; of the element. So this example add two strings within the paragraph tags: <strong>$(&#8216;#someP&#8217;).append(&#8216;This is at the end.&#8217;).prepend(&#8216;This is at the beginning.&#8217;)</strong>. Both methods have a corresponding <strong>*To()</strong> method—<strong>appendTo()</strong> and <strong>prependTo()</strong>—for situations where you want to create new content, then choose the existing element to which the new content should be added.</p><p>The corollaries to <strong>append()</strong> and <strong>prepend()</strong> are <strong>after()</strong> and <strong>before()</strong>. These methods add the new content outside of the selected element. The <strong>insertAfter()</strong> and <strong>insertBefore()</strong> work like <strong>appendTo()</strong> and <strong>prependTo()</strong>, but again are applied outside of the existing element.</p><p>Next up are a series of methods for wrapping elements. For example, if you want to wrap all paragraphs in a DIV: <strong>$(&#8216;p&#8217;).wrap(&#8216;&lt;div class=&#8221;content&#8221;&gt;&lt;/div&gt;&#8217;)</strong>. There are variations on this method; see the<a
href="http://docs.jquery.com/Manipulation"> jQuery documentation</a> for more.</p><p>Finally, there&#8217;s the <strong>replaceWith()</strong> method, for replacing an element with something else, like replacing all unordered lists with ordered lists. The <strong>empty()</strong> method gets rid of an elements children (aka subelements) and <strong>remove()</strong> gets rid of the element itself, plus its children.</p><p>So that&#8217;s an overview of how you can manipulate DOM elements. I didn&#8217;t rough out an example for these but I can if you think further clarification is necessary. In my next post, I&#8217;ll talk about making Ajax requests using jQuery.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2009/03/15/a-quick-intro-to-jquery-part-5/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <series:name><![CDATA[Introduction to jQuery]]></series:name> </item> <item><title>Simple, Built-In jQuery Effects</title><link>http://www.larryullman.com/2009/03/05/a-quick-intro-to-jquery-part-4/</link> <comments>http://www.larryullman.com/2009/03/05/a-quick-intro-to-jquery-part-4/#comments</comments> <pubDate>Fri, 06 Mar 2009 02:20:19 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[ajax]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[jquery]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=298</guid> <description><![CDATA[If you&#8217;ve been reading my jQuery series of posts sequentially, then you saw , learned so that you can use or manipulate them, and hopefully grasped . In this post, I go into the effects you can apply to add dynamic behavior to an HTML page. Note that I&#8217;m discussing just the out-of-the-box effects built [...]]]></description> <content:encoded><![CDATA[<p>If you&#8217;ve been reading my <a
href="http://www.jquery.com">jQuery</a> series of posts sequentially, then you saw <a
href="http://www.larryullman.com/2009/02/05/a-quick-intro-to-jquery/">how to incorporate jQuery into an HTML page</a>, learned <a
href="http://www.larryullman.com/2009/02/07/a-quick-intro-to-jquery-part-2/">how to select elements</a> so that you can use or manipulate them, and hopefully grasped <a
href="http://www.larryullman.com/2009/03/02/a-quick-intro-to-jquery-part-3/">how to add event handlers</a>. In this post, I go into the effects you can apply to add dynamic behavior to an HTML page. Note that I&#8217;m discussing just the out-of-the-box effects built into jQuery. A subsequent post will discuss some of the plug-ins that can be brought into a page.<span
id="more-298"></span></p><p>In my second post on jQuery, I already mentioned a couple of functions you can use for various effects: <strong>addClass()</strong>, <strong>hide()</strong>, and <strong>show()</strong>. Of these, <strong>addClass()</strong> and its sibling <strong>removeClass()</strong> is the most powerful and flexible. By applying defined CSS classes to elements (or by removing their application), you can dramatically change the appearance of an element. And as CSS allows elements to have multiple classes, so you can add as many as you&#8217;d like.</p><p>Moving on, the <strong>hide()</strong> and <strong>show()</strong> methods do exactly what you&#8217;d expect. Each takes an optional speed argument: <em>slow</em>, <em>normal</em>, or <em>fast</em>. Or you could specify how quickly the effect is accomplished by providing a number of milliseconds. The default affect, without a speed argument, is for the transition to occur immediately.</p><p>These two methods can be combined under one umbrella using <strong>toggle()</strong>. Say you have a title and some content:</p><pre class="brush: jscript; title: ; notranslate">&lt;h2 id=&quot;moreTitle&quot;&gt;Click for More Information&lt;/h2&gt;
&lt;p id=&quot;moreInfo&quot;&gt;Lorem ipsum dolor sit amet....&lt;/p&gt;</pre><p>You can add a <strong>toggle()</strong> method to the paragraph that&#8217;s revealed and hidden when the H2 is clicked:</p><pre class="brush: jscript; title: ; notranslate">$('#moreTitle').click(function() {
    $('#moreInfo').toggle();
});</pre><p>If the P is hidden, then clicking the title will show it. If it&#8217;s currently shown, clicking the title will hide it. You can see this in action in the second section in the example. And, as you can see in the example, the rest of the page content gets moved around seamlessly as needed. (Note that in a real application, you&#8217;d want to make it all look nicer, perhaps having a +/- graphic that changes to indicate the state.)</p><p>As an aside, going back to the <strong>addClass()</strong> and <strong>removeClass()</strong> methods, there&#8217;s also <strong>toggleClass()</strong>, which adds or removes a class as appropriate.</p><p>Similar to <strong>show()</strong> and <strong>hide()</strong> are <strong>slideUp()</strong>, <strong>slideDown()</strong>, and <strong>slideToggle()</strong>. The effect is the same as using <strong>show()</strong> and <strong>hide()</strong> with a set speed. See the third example on the demo page.</p><p>Next there&#8217;s <strong>fadeIn()</strong> and <strong>fadeOut()</strong>. Each makes objects appear or disappear, but only through the effect of fading (see the fourth section in the demo). Both take an optional speed argument, which is again <em>slow</em>, <em>normal</em>, <em>fast</em>, or a number of milliseconds. The <strong>fadeTo()</strong> method changes the opacity of an object. Provide it with a speed as the first argument and the desired opacity as the second. This should be a number from 0.0 (hidden) to 1.0 (full visibility).</p><p>Again, see the demo example that goes with this post to view these in action, and look at the source code to see the minimal amount of JavaScript needed to make all this happen. The stress in the demo is on the functionality; real-world implementation would naturally be improved.</p><p>A few last things, to wrap this up&#8230;</p><ul><li>An entirely different way you can affect an HTML page is to change the elements that exist (i.e., add and remove objects) or change their contents. Both will be discussed in the next post on jQuery.</li><li>You can write custom effects and apply them to elements using the animate() method.</li><li>The stop() method terminates an in-process effect.</li><li>Because of the way jQuery methods work (they often return the thing they were applied to), you can chain multiple methods in one line, thereby creating multiple effects in one step. This next line makes it so that any time a link is clicked, it&#8217;s class is changed and it&#8217;s partially faded:</li></ul><pre class="brush: jscript; title: ; notranslate">$('a').click(function() { $(this).addClass('someNewClass').fadeTo(0.4); });</pre><ul><li>Finally, for all of these effects, remember that jQuery lets you select any number of objects on the page at one time, so you can apply one or more effects to one or more elements at the same time (as again the above line of code demonstrates).</li></ul><p>UPDATE: I keep forgetting to mention an important jQuery concept here: jQuery will not complain if you attempt to apply an inappropriate method. For example, if you apply <strong>hide()</strong> to a hidden element, nothing happens, including no errors or exceptions. Or if you call <strong>removeClass()</strong> on an element that doesn&#8217;t have the named class, again, it&#8217;s not a problem. This is important to know because it means you don&#8217;t need to add a ton of code checking for an element&#8217;s properties prior to attempting something (as you should in JavaScript proper or in other programming languages).</p><p>So that&#8217;s it for built-in jQuery effects. I hope this helped and let me know if you have any comments or questions.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2009/03/05/a-quick-intro-to-jquery-part-4/feed/</wfw:commentRss> <slash:comments>4</slash:comments> <series:name><![CDATA[Introduction to jQuery]]></series:name> </item> <item><title>Event Handling in jQuery</title><link>http://www.larryullman.com/2009/03/02/a-quick-intro-to-jquery-part-3/</link> <comments>http://www.larryullman.com/2009/03/02/a-quick-intro-to-jquery-part-3/#comments</comments> <pubDate>Mon, 02 Mar 2009 20:22:29 +0000</pubDate> <dc:creator>Larry</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[ajax]]></category> <category><![CDATA[framework]]></category> <category><![CDATA[jquery]]></category><guid
isPermaLink="false">http://www.larryullman.com/?p=261</guid> <description><![CDATA[In the first two posts on jQuery, I discuss its basic usage on a page and its selectors. That basic knowledge—from me or elsewhere—is required to continue reading here. In this post, I get into event handling in jQuery. With standard JavaScript, events like onclick and onmouseover are added to page elements, either in a [...]]]></description> <content:encoded><![CDATA[<p>In the first two posts on jQuery, I discuss its  basic usage on a page and its selectors. That basic knowledge—from me or elsewhere—is required to continue reading here. In this post, I get into event handling in jQuery. <span
id="more-261"></span>With standard JavaScript, events like <em>onclick</em> and <em>onmouseover</em> are added to page elements, either in a script tag or in the element&#8217;s tag, like this:</p><pre class="brush: jscript; title: ; notranslate">&lt;input type=&quot;text&quot; id=&quot;something&quot; onchange=&quot;callThisFunction()&quot; /&gt;</pre><p>In jQuery, the preference is to put the JavaScript within SCRIPT tags in the HEAD of the page, not lost in the rest of the HTML body (this is part of the <a
href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript">Unobtrusive JavaScript</a> philosophy). So you would first select a particular element using <strong>$(&#8216;#something&#8217;)</strong> or whatever, then call the method associated with the desired event. The <em>onchange</em> in the above code becomes <strong>change()</strong>, <em>onfocus</em> becomes <strong>focus()</strong>, <em>onmouseover</em> is <strong>mouseover()</strong>, <em>onclick</em> becomes <strong>click()</strong>, etc. Normally each of these is passed an anonymous function to be executed when the event occurs:</p><pre class="brush: jscript; title: ; notranslate">$('#something').change(function() {
    // Do whatever.
});</pre><p>Within the anonymous function, <strong>this</strong> refers to the element that&#8217;s the recipient of the event. So to alert what the user just entered into the text box, you would code:</p><pre class="brush: jscript; title: ; notranslate">$('#something').change(function() {
    alert($(this).val());
});</pre><p>Also, to be clear, the <strong>val()</strong> method returns the value of an element. With a text input, the value is the text entered into it.</p><p>Note that because you can&#8217;t reference Document Object Model elements until after the DOM has loaded, you&#8217;d put this code into a function that&#8217;s called after that point:</p><pre class="brush: jscript; title: ; notranslate">$(function() {
    $('#something').change(function() {
        alert($(this).val());
    });
});</pre><p>As I discussed in my first post on jQuery, the above syntax is shorthand for</p><pre class="brush: jscript; title: ; notranslate">$(document).ready(function() {
    // Do whatever.
});</pre><p>What this means is that <strong>ready()</strong> is a method associated with an event, specifically the browser window&#8217;s event of the DOM having been loaded.</p><p>There are other ways in jQuery to associate events with elements. Two nice methods—<strong>hover()</strong> and <strong>toggle()</strong>—take multiple functions as arguments. The <strong>hover()</strong> method takes a mouseover function as its first argument, the mouseout function as its second. So using it you can easily invoke whatever code with this common event pair. The <strong>toggle()</strong> method takes two or more functions as arguments. Each subsequent function will be executed upon repeating clicks upon that element. Also, I should point out that the <strong>bind()</strong> method is an alternative way of associating events with elements. This method takes the event type as its first argument, the function to be called as its second. The above example could be rewritten as:</p><pre class="brush: jscript; title: ; notranslate">$('#something').bind('change', (function() {
    alert($(this).val());
});</pre><p>The jQuery documentation discusses all of the <a
href="http://docs.jquery.com/Events">event-related methods</a>, of course. There are several related to key events, and other important ones like: <strong>submit()</strong>, for when a form is submitted; <strong>select()</strong>, for when an element is selected; and, <strong>resize()</strong>, for when the browser window is resized. They&#8217;re all well documented, with examples. And, of course, these are all browser-independent ways of handling events in jQuery.</p><p>In the next post, I plan on discussing some of jQuery&#8217;s effects, like hiding and showing elements.</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2009/03/02/a-quick-intro-to-jquery-part-3/feed/</wfw:commentRss> <slash:comments>2</slash:comments> <series:name><![CDATA[Introduction to jQuery]]></series:name> </item> </channel> </rss>
<!-- Served from: www.larryullman.com @ 2012-05-21 14:23:47 by W3 Total Cache -->
