Just wondering...

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?
 
P

peter.chase

DoomedLung said:
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?


Because array indexes start at zero. He is getting the element of
"classes" that corresponds to the last cell of the row.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top