you’re on your way 4
111
ajax requests
With the address from the server, all that’s left is to update the web
form. Since the address is stored in a form eld, you can use the
DOM again, similar to how you got the value of the phone number
eld in getCustomerInfo() way back on page 82.
function updatePage() {
if (request.readyState == 4) {
/* Get the response from the server */
var customerAddress = request.responseText;
/* Update the HTML web form */
document.getElementById(“address”).value =
customerAddress;
}
}
Since address is a form eld,
you can access the text in it
using the “value” property.
Here’s where
those id
attributes come
in handy again.
Q:
Why don’t we have to use that JavaScript utility file
from Chapter 1 to update the address field?
a: In Chapter 1, we were updating the text in a <span>
element. Since <span> isn’t an HTML element that you usually
type in, it doesn’t have a value property; the same is true for
most other HTML elements, like <p>, <em>, and <div>.
For form fields, though, you usually do need to enter a value.
To make that easier, you can just use the value property on a
form field element, and get and set its text value directly.
Q:
Couldn’t we just use a <div> for the customer’s
address? Why are we using a field, anyway?
a: Even though the Break Neck server looks up the
customer’s address, there are times when a customer might
want their pizza deliv ...