Java Reference
In-Depth Information
After you build the description text, you select the element serving as the description container and set
its text using jQuery's text() method.
As you can see from this example, jQuery simplifies DOM manipulation and event handling. In this
particular example, you wrote less JavaScript to attain the same results. That's well worth the time of
learning jQuery, isn't it?
But that's not all; jQuery can do the same thing for your Ajax code.
using jQuery for ajax
In Chapter 14, you learned about Ajax and how asynchronous requests require you to write a lot of
extra code. You wrote a simple utility to help alleviate the complexity of Ajax code, but jQuery can
simplify Ajax even more.
Understanding the jQuery Function
The jQuery function ( $() ) is the doorway into all things jQuery, and you've used it quite a bit
throughout this chapter. However, this function has other uses.
Functions are objects and they have a property called prototype . Like all other objects, you access
a Function object's properties and methods using the object.property or object.method()
syntax. Well, jQuery's $ function has many methods, and some of them are for making Ajax
requests. One of them is the get() method, which is for making GET requests. The following code
shows an example:
$.get("textFile.txt");
This code makes a GET request to the server for the textFile.txt file. But this code isn't very
useful because it doesn't do anything with the server's response. So like the HttpRequest module
you built in Chapter 14, the $.get() method lets you define a callback function that handles the
response from the server:
function handleResponse(data) {
alert(data);
}
$.get("textFile.txt", handleResponse);
This code defines a function called handleResponse() and passes it to $.get() . jQuery calls this
function on a successful request and passes it the requested data (represented by the data parameter).
Remember the examples from Chapter 14? You created a form that checked if usernames and e‐mail
addresses were available using Ajax, and you sent those values to the server as parameters in the
URL. For example, when you wanted to test a username, you used the username parameter, like
this:
phpformvalidator.php?username=jmcpeak
 
Search WWH ::




Custom Search