Skip to Content
Head Rush Ajax
book

Head Rush Ajax

by Brett McLaughlin
March 2006
Beginner to intermediate
448 pages
13h 33m
English
O'Reilly Media, Inc.
Content preview from Head Rush Ajax
168
Chapter 3
Setting the text in a <div>
Now that you know how to get the text in the coffee maker status
<div>s, you just need to be able to set the text in those status
<div>s. You can use another utility function in text-utils.js
for this, called replaceText().
changing a <div>’s text
JavaScript
PHP script
HTML form
function orderCoffee() {
var name = document.getElementById(“name”).value;
var beverage = getBeverage();
var size = getSize();
var coffeemakerStatusDiv1 =
document.getElementById(“coffeemaker1-status”);
var status = getText(coffeemakerStatusDiv1);
if (status == “Idle”) {
replaceText(coffeemakerStatusDiv1, “Brewing “ +
name + “‘s “ +
size + “ “ + beverage);
var url = “coffeemaker.php?name=” + escape(name) +
“&size=” + escape(size) +
“&beverage=” + escape(beverage) +
“&coffeemaker=1”;
sendRequest(url);
}
}
If the rst coffee maker is
idle, this will update the coffee
maker’s status to indicate that
it’s making a drink...
...and these send the request to
the coffee-making PHP script.
text-utils.js
getText()
replaceText()
getText() will return the text
within a <div>, or any other
element that you give it.
reaplaceText() takes an element,
and the text to put within that
element. This will clear out any
existing text, so that the text
you supply becomes the only text
in the element.
you’re on your way 4
169
asynchronous applications
...at ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

What Employees Want Most in Uncertain Times

What Employees Want Most in Uncertain Times

Kristine W. Powers, Jessica B.B. Diaz
What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
How to Become a Game-Changing Leader

How to Become a Game-Changing Leader

Douglas A. Ready, Alan Mulally

Publisher Resources

ISBN: 0596102259Errata PageSupplemental Content