Prev | Current Page 108 | Next

Larry Ullman

"Building a Web Site with Ajax: Visual QuickProject Guide"


??¦
while (results.hasChildNodes()) {
results.removeChild(results.lastChild);
}
var data = eval(???(??? + ajax.responseText + ???)??™);
if (data.length > 0) {
??¦
136 Appendix
using JSON data
To update the DOM using the JSON data, complete the modifi cation of the
handleResponse() function:
1 Access every returned record using a loop.
??¦
if (data.length > 0) {
var employee, span, name_node, dept_node, dept_
label, br, strong, a, email;
for (var i = 0; i < data.length; i++) {
employee = document.createElement(???p??™);
span = document.createElement(???span??™);
span.setAttribute(???class??™, ???name??™);
Appendix 137
using JSON data (cont.)
2 Access the individual elements using dot syntax.
name_node = document.createTextNode(data[i].name);
span.appendChild(name_node);
employee.appendChild(span);
br = document.createElement(???br??™);
employee.appendChild(br);
strong = document.createElement(???strong??™);
dept_label = document.createTextNode(???Department??™)
;
strong.appendChild(dept_label);
employee.appendChild(strong);
dept_node = document.createTextNode(???: ??? +
data[i].


Pages:
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120