D
DoomedLung
I've been reading the DHTML Reference (2nd edt.) book by Danny Goodman.
I've come to a point in the book where my scripting experience isn't
that great to understand some of the coding examples and was just
wondering if anyone could help.
I'm at chapter 5 section 'Dynamic Tables' this is the example I'm a
little confused with...
function drawTable(tbody) {
var tr, td, i, j, oneRecord;
tbody = document.getElementById(tbody);
// node tree
var data = xDoc.getElementsByTagName("season")[0];
// for td class attributes
var classes = ["ctr","","","","ctr"];
for (i = 0; i < data.childNodes.length; i++) {
// use only 1st level element nodes
if (data.childNodes.nodeType == 1) {
// one bowl record
oneRecord = data.childNodes;
tr = tbody.insertRow(tbody.rows.length);
td = tr.insertCell(tr.cells.length);
td.setAttribute("class",classes[tr.cells.length-1]);
td.innerHTML =
oneRecord.getElementsByTagName("number")[0].firstChild.nodeValue;
td = tr.insertCell(tr.cells.length);
td.setAttribute("class",classes[tr.cells.length-1]);
td.innerHTML =
oneRecord.getElementsByTagName("year")[0].firstChild.nodeValue;
td = tr.insertCell(tr.cells.length);
td.setAttribute("class",classes[tr.cells.length-1]);
td.innerHTML =
oneRecord.getElementsByTagName("winner")[0].firstChild.nodeValue;
td = tr.insertCell(tr.cells.length);
td.setAttribute("class",classes[tr.cells.length-1]);
td.innerHTML =
oneRecord.getElementsByTagName("loser")[0].firstChild.nodeValue;
td = tr.insertCell(tr.cells.length);
td.setAttribute("class",classes[tr.cells.length-1]);
td.innerHTML =
oneRecord.getElementsByTagName("winscore")[0].firstChild.nodeValue + "
- " +
oneRecord.getElementsByTagName("losscore")[0].firstChild.nodeValue;
}
}
}
Obviously this function draws a table and fills the cells with content
from an XML doc this line from the function is where I'm getting
confused:
td.setAttribute("class",classes[tr.cells.length-1]);
Why is there a -1 in the Statement?
I've come to a point in the book where my scripting experience isn't
that great to understand some of the coding examples and was just
wondering if anyone could help.
I'm at chapter 5 section 'Dynamic Tables' this is the example I'm a
little confused with...
function drawTable(tbody) {
var tr, td, i, j, oneRecord;
tbody = document.getElementById(tbody);
// node tree
var data = xDoc.getElementsByTagName("season")[0];
// for td class attributes
var classes = ["ctr","","","","ctr"];
for (i = 0; i < data.childNodes.length; i++) {
// use only 1st level element nodes
if (data.childNodes.nodeType == 1) {
// one bowl record
oneRecord = data.childNodes;
tr = tbody.insertRow(tbody.rows.length);
td = tr.insertCell(tr.cells.length);
td.setAttribute("class",classes[tr.cells.length-1]);
td.innerHTML =
oneRecord.getElementsByTagName("number")[0].firstChild.nodeValue;
td = tr.insertCell(tr.cells.length);
td.setAttribute("class",classes[tr.cells.length-1]);
td.innerHTML =
oneRecord.getElementsByTagName("year")[0].firstChild.nodeValue;
td = tr.insertCell(tr.cells.length);
td.setAttribute("class",classes[tr.cells.length-1]);
td.innerHTML =
oneRecord.getElementsByTagName("winner")[0].firstChild.nodeValue;
td = tr.insertCell(tr.cells.length);
td.setAttribute("class",classes[tr.cells.length-1]);
td.innerHTML =
oneRecord.getElementsByTagName("loser")[0].firstChild.nodeValue;
td = tr.insertCell(tr.cells.length);
td.setAttribute("class",classes[tr.cells.length-1]);
td.innerHTML =
oneRecord.getElementsByTagName("winscore")[0].firstChild.nodeValue + "
- " +
oneRecord.getElementsByTagName("losscore")[0].firstChild.nodeValue;
}
}
}
Obviously this function draws a table and fills the cells with content
from an XML doc this line from the function is where I'm getting
confused:
td.setAttribute("class",classes[tr.cells.length-1]);
Why is there a -1 in the Statement?