Why jQuery?
Updated 2009-07
jQuery is appealing because: (1) it uses CSS, XPath, and custom selectors to work it's magic, and (2) the API is very similar to Perl.
Getting Started
First load your jQuery code. Instead of:
window.onload = function(){ ... }
to start working with the document object model (DOM) and adding events, use the jQuery shortcut:
$(document).ready(function(){
// your javascript code
});
Selectors
To select an element with plain Javascript:
var element = document.getElementById('ELEMENT_ID');
In jQuery
var element = $('ELEMENT_ID');
which actually creates a new jQuery object to work with. In this page we're using a single DIV container to show the updates, and DOM and CSS to display the results. Look through this page's HTML page source:
<div id='prototype_wrapper'
style='position:absolute;display:none;z-index:100;
width:240px;padding:1.0em 3px;border:1px solid #000
;background:#ffffcc;'>
<div id='jq_content'></div>
<div id='systemWorking'>
<img src='/kyouyuu/image/ajax-processing-small.gif'>
Loading...
</div>
</div>
Notes:
- The first (outermost) DIV container is used to show/hide the results from the Ajax call on onmouseover/onmouseout.
- The second DIV container holds the Ajax query results as HTML.
- The third DIV container shows a status image to let the user know their request is being processed. For demonstration, the page processing the Ajax request has an explicit half-second
Thread.Sleep() call to simulate a long running process.
Javascript jQuery Style
Now we can get to the jQuery. To work with the three DIV containers above we create a new jQuery object for each using the jQuery selector:
var jq_wrapper = $('#jq_wrapper');
var jq_content = $('#jq_content');
var jq_working = $('#systemWorking');
We definitely want to make sure any error conditions (
ajaxError() ) are handled and show a friendly message to the client:
jq_content.ajaxError(function(request, settings) {
$(this).html('<span class="red">Error requesting page');
});
To give the client an indication the page is loading on long running processes, we use the
ajaxStart() method to bind the function that shows the spinning gif when the AJAX request begins, and the
ajaxStop() method to bind the function that hides the spinning gif when all AJAX requests have ended:
jq_working.ajaxStart(function(){
jq_content.hide();
$(this).show();
});
jq_working.ajaxStop(function(){
$(this).hide();
jq_content.show();
});
Positioning the Data
jQuery saves you a ton of work trying to get the height/width and top/left offset of any element that is cross-browser compliant. Since we want the query results positioned near the hyperlink, that's exactly what we need:
var cur_dim = $(this).offset();
var win_h = $(window).height();
var win_w = $(window).width();
var this_h = cur_dim.top;
var this_w = cur_dim.left;
var ctop = win_h / 2 >= this_h ? this_h : this_h - 50;
var cleft = win_w / 2 >= this_w
? this_w + $(this).width()
: this_w - jq_wrapper.width() - $(this).width();
Ajax Call
The calculations above will be used in the function body used for the mouseover event that does the Ajax call:
// jQuery selector; hyperlinks with 'class' attribute set to 'jq_tt'
$('a.jq_tt').mouseover(function() {
jq_wrapper.show(); // show Ajax response results
/* top/left calculation code above goes here */
// and set here
jq_wrapper.css( {top:ctop, left:cleft} );
// Ajax request/response processing
jq_content.load($(this).attr('href'),
function(responseText, stat, response) {
// only show **successful** responses; our error handler above
// will show a friendly message otherwise
if (stat == 'success') {
jq_content.html(responseText);
}
}
);
});
Notes:
- DO use the load() method if you are returning HTML. Although the documentation doesn't specify it, I found out the hard way that the paramaters passed determine whether
POST/GET data is sent. (pass a hashmap as the second parameter for POST) -
- Yes, that means I also wasted a bit of time first trying to use the ajaxSend() method to dump HTML into the DIV container showing the query results. The problem is that
ajaxSend() is working with XML, not HTML, and will dump the responseText text value. (stripping all tags)
All that's left is to hide the results on mouseout:
$('a.jq_tt').mouseout(function() {
jq_wrapper.hide();
});

Loading...
The results:
| No | Name | City | State | Born | Died |
| 1 | George Washington | Wakefield | VA | Feb 22, 1732 | Dec 14, 1799 |
| 2 | John Adams | Braintree | MA | Oct 30, 1735 | Jul 04, 1826 |
| 3 | Thomas Jefferson | Albemarle County | VA | Apr 13, 1743 | Jul 04, 1826 |
| 4 | James Madison | Port Conway | VA | Mar 16, 1751 | Jun 28, 1836 |
| 5 | James Monroe | Westmoreland County | VA | Apr 28, 1758 | Jul 04, 1831 |
| 6 | John Quincy Adams | Braintree | MA | Jul 11, 1767 | Feb 23, 1848 |
| 7 | Andrew Jackson | Waxhaw settlement | SC | Mar 15, 1767 | Jun 08, 1845 |
| 8 | Martin Van Buren | Kinderhook | NY | Dec 05, 1782 | Jul 24, 1862 |
| 9 | William H. Harrison | Berkeley | VA | Feb 09, 1773 | Apr 04, 1841 |
| 10 | John Tyler | Greenway | VA | Mar 29, 1790 | Jan 18, 1862 |
| 11 | James K. Polk | Pineville | NC | Nov 02, 1795 | Jun 15, 1849 |
| 12 | Zachary Taylor | Orange County | VA | Nov 24, 1784 | Jul 09, 1850 |
| 13 | Millard Fillmore | Locke | NY | Jan 07, 1800 | Mar 08, 1874 |
| 14 | Franklin Pierce | Hillsboro | NH | Nov 23, 1804 | Oct 08, 1869 |
| 15 | James Buchanan | Mercersburg | PA | Apr 23, 1791 | Jun 01, 1868 |
| 16 | Abraham Lincoln | Hodgenville | KY | Feb 12, 1809 | Apr 15, 1865 |
| 17 | Andrew Johnson | Raleigh | NC | Dec 29, 1808 | Jul 31, 1875 |
| 18 | Ulysses S. Grant | Point Pleasant | OH | Apr 27, 1822 | Jul 23, 1885 |
| 19 | Rutherford B. Hayes | Delaware | OH | Oct 04, 1822 | Jan 17, 1893 |
| 20 | James A. Garfield | Orange | OH | Nov 19, 1831 | Sep 19, 1881 |
| 21 | Chester A. Arthur | Fairfield | VT | Oct 05, 1829 | Nov 18, 1886 |
| 22 | Grover Cleveland | Caldwell | NJ | Mar 18, 1837 | Jun 24, 1908 |
| 23 | Benjamin Harrison | North Bend | OH | Aug 20, 1833 | Mar 13, 1901 |
| 24 | William McKinley | Niles | OH | Jan 29, 1843 | Sep 14, 1901 |
| 25 | Theodore Roosevelt | New York | NY | Oct 27, 1858 | Jan 06, 1919 |
| 26 | William H. Taft | Cincinnati | OH | Sep 15, 1857 | Mar 08, 1930 |
| 27 | Woodrow Wilson | Staunton | VA | Dec 19, 1856 | Feb 03, 1924 |
| 28 | Warren G. Harding | Blooming Grove | OH | Nov 02, 1865 | Aug 02, 1923 |
| 29 | Calvin Coolidge | Plymouth Notch | VT | Jul 04, 1872 | Jan 05, 1933 |
| 30 | Herbert C. Hoover | West Branch | IA | Aug 10, 1874 | Oct 20, 1964 |
| 31 | Franklin D. Roosevelt | Hyde Park | NY | Jan 30, 1882 | Apr 12, 1945 |
| 32 | Harry S Truman | Lamar | MO | May 08, 1884 | Dec 26, 1972 |
| 33 | Dwight D. Eisenhower | Denison | TX | Oct 14, 1890 | Mar 28, 1969 |
| 34 | John F Kennedy | Brookline | MA | May 29, 1917 | Nov 22, 1963 |
| 35 | Lyndon B. Johnson | Stonewall | TX | Aug 27, 1908 | Jan 22, 1973 |
| 36 | Richard M Nixon | Yorba Linda | CA | Jan 09, 1913 | Apr 22, 1994 |
| 37 | Gerald R Ford | Omaha | NE | Jul 14, 1913 | Dec 26, 2006 |
| 38 | James E. Carter Jr. | Plains | GA | Oct 01, 1924 | |
| 39 | Ronald W. Reagan | Tampico | IL | Feb 06, 1911 | Jun 05, 2004 |
| 40 | George H.W. Bush | Milton | MA | Jun 12, 1924 | |
| 41 | William J. Clinton | Hope | AR | Aug 19, 1946 | |
| 42 | George W. Bush | New Haven | CT | Jul 06, 1946 | |
jQuery Links