<?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>Sun, 05 Feb 2012 17:48:42 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <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/">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> <item><title>Selecting Elements in jQuery</title><link>http://www.larryullman.com/2009/02/07/a-quick-intro-to-jquery-part-2/</link> <comments>http://www.larryullman.com/2009/02/07/a-quick-intro-to-jquery-part-2/#comments</comments> <pubDate>Sat, 07 Feb 2009 20:10:02 +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=248</guid> <description><![CDATA[, I discuss how to include the jQuery library in your HTML page and how to have JavaScript executed after the document has loaded. In this next post, I&#8217;m going to talk about referencing Document Object Model (DOM) elements using selectors. As much of using JavaScript in an HTML page involves manipulating the DOM, this [...]]]></description> <content:encoded><![CDATA[<p><a
href="http://www.larryullman.com/2009/02/05/a-quick-intro-to-jquery/"></a><a
href="http://www.larryullman.com/2009/02/05/a-quick-intro-to-jquery/">In my first post on jQuery</a>, I discuss how to include the <a
href="http://www.jquery.com">jQuery</a> library in your HTML page and how to have JavaScript executed after the document has loaded. In this next post, I&#8217;m going to talk about referencing Document Object Model (DOM) elements using <em>selectors</em>. As much of using JavaScript in an HTML page involves manipulating the DOM, this is an important concept to get down.<span
id="more-248"></span>To manipulate any DOM element using jQuery, you have to first identify it, which is where the <span
style="text-decoration: underline;">selector</span> comes in. In standard JavaScript, you&#8217;d likely use the <strong>getElementById()</strong>, <strong>getElementByClassName()</strong>, <strong>getElementByTag()</strong>, and <strong>getElementByTagName()</strong> functions, applied to the document object, to reference page elements. In jQuery, you&#8217;d use the<strong> jQuery()</strong> function, which is aliased as <strong>$()</strong>.This syntax looks quite odd and daunting at first, but it&#8217;s extremely simple. If you have a DOM element with an ID value of <em>something</em>, then <strong>$(&#8216;#something&#8217;)</strong> is jQuery&#8217;s way of pinpointing it. This unusual <strong>$()</strong> syntax is just shorthand for the <strong>jQuery()</strong> function (a dollar sign being much shorter to type). If you read the previous post, then you&#8217;ve already seen this construct:</p><pre class="brush: jscript; title: ; notranslate">$(document).ready(function() {
    // Do whatever.
});</pre><p>The <strong>$(document)</strong> part refers to the HTML document (note that there are no quotes in this document reference), so that code selects the document, on which the <strong>ready()</strong> method is called.</p><p>Selectors in jQuery work like selectors in CSS. You can identify a specific element using its ID value, as in the above example. You can also identify every element with a given class: <strong>$(&#8216;.someClass&#8217;)</strong> refers to any element type that has a <strong>class</strong> value of <em>someClass</em>. Or you can identify every element of a given type: <strong>$(&#8216;div&#8217;)</strong> refers to all DIV elements and <strong>$(&#8216;ul li&#8217;)</strong> refers to all list elements found within unordered lists (so it wouldn&#8217;t select list elements in ordered lists). You can even combine these: to reference all the table rows with a <strong>class</strong> of <em>heading</em>, use <strong>$(&#8216;tr.heading&#8217;)</strong>.</p><p><a
href="http://docs.jquery.com/Selectors">Selectors</a> are very important and powerful. I found the syntax to be cryptic and confusing at first, but once I realized how simple it was, jQuery really opened up for me. The examples I provided are relatively obvious, but jQuery has its own unique selectors. For example, you could refer to every other row in an HTML table using <em>tr:nth-child(even)</em> (you might do this to alternate the row background colors). Or you can find elements based upon their parent elements, like every image that&#8217;s a child object of DIV: <em>div &gt; img</em> (that would not find images placed outside of DIVs or nested within subelements of a DIV).</p><p>Once you&#8217;ve selected an element or list of elements using <strong>$()</strong>, you can apply methods to them to change their behavior. Object-oriented programming languages (like JavaScript) use a <em>objectName.methodName</em> syntax to call a function on an object (the function must &#8220;belong&#8221; to that object type, though). In jQuery, it&#8217;s the same thing. For example, to apply a new CSS class to all paragraphs, you would write <strong>$(&#8216;p&#8217;).addClass(&#8216;someClass&#8217;)</strong>. Or say you want to make a specific object disappear: <strong>$(&#8216;#someID&#8217;).hide()</strong>. If you need to change an element&#8217;s HTML, like the text found between paragraph tags, call the <strong>html()</strong> method, passing it the new value: <strong>$(&#8216;#update&#8217;).html(&#8216;New text.&#8217;)</strong>.</p><p>Again, as with some object-oriented languages, you can <span
style="text-decoration: underline;">chain</span> multiple methods, so that the result from one method is then applied to another: <em>objectName.methodName1.methodName2.methodName3</em>. (How well and to what level this is supported differs from one OOP language to the next, but this is the kind of thing that Ruby does very well and is also supported in jQuery.) So, as an example, say you create a page of questions with the answers hidden in SPAN tags around the document. They all have the <strong>class</strong> <em>answer</em> and are all initially hidden. The user looks at each question, thinks about their answers, then clicks a button or whatever that reveals the right answers. To do this, you might want to both display all of these SPANs and add an extra class to highlight them (elements in HTML can have multiple classes). In JavaScript, this would require a lot of code but in jQuery, it&#8217;d just be <strong>$(&#8216;span.answer&#8217;).addClass(&#8216;highlight&#8217;).show()</strong>.</p><p>Once you&#8217;ve grasped the jQuery syntax and know how to select the element(s) to which something should apply, then it&#8217;s just a matter of invoking the proper methods on the selected element(s). You can find the full documentation for all of the available effects/methods in the <a
href="http://docs.jquery.com/Main_Page">jQuery documentation</a>, of course. That Web site also has quite a few <a
href="http://docs.jquery.com/Tutorials">well-written and useful tutorials</a> that you ought to consider.</p><p>In my next post, I&#8217;ll discuss event handling, which is what you also need to know to apply this knowledge (because you almost always want to change something on the page only after something specific has happened).</p> ]]></content:encoded> <wfw:commentRss>http://www.larryullman.com/2009/02/07/a-quick-intro-to-jquery-part-2/feed/</wfw:commentRss> <slash:comments>3</slash:comments> <series:name><![CDATA[Introduction to jQuery]]></series:name> </item> </channel> </rss>
<!-- Served from: www.larryullman.com @ 2012-02-05 15:35:15 by W3 Total Cache -->
