Using the DOM to calculate and display averages

M

Mick White

http://www.mickweb.com/football/aleague/ratings.html

I can't get this to work in IE5.2 Mac, it seems to choke on assigning a
table cell's innerHTML property.

player.item(player.length-1).innerHTML=av;

I'm attempting to add each player's ratings and calculate his average
rating. Appears to work in Safari 1.0.2, Netscape 7.0, and Firefox 0.9
(all Mac).
Can anyone suggest how I can use the heirarchal(nodal) DOM to accomplish
the same end? Or improve the script? All table elements are in place.
Mick

Full script below:
<script type="text/JavaScript">
onload=function (){
if(document.getElementsByTagName){
rs= document.getElementsByTagName("TR");
r=rs.length;
while(r--){
if(r&1){
rs.item(r).style.backgroundColor="#c3d3d3";
}
}
//trouble begins below
var t=document.getElementById("ratings");// The table
var trs=t.getElementsByTagName("TR");
for(b=2;b<trs.length;b++){// skip the first two rows
var player=trs.item(b).getElementsByTagName("TD");
var z=0,q=0;
if(player.length){
for(x=0;x<player.length;x++){
if(!isNaN(parseFloat(player.item(x).innerHTML))){
z+=(+ player.item(x).innerHTML);// seems to work
q++ ;
}
av=Math.round((z/q)*100)/100;// so far so good
}
player.item(player.length-1).innerHTML=av; // not good
player.item(player.length-1).style.fontWeight="bold";
}
}
}
}
</script>
 
M

Martin Honnen

Mick White wrote:

av=Math.round((z/q)*100)/100;// so far so good
}
player.item(player.length-1).innerHTML=av; // not good

You need to insert a text node, in general after removing all child
nodes thus try
function setInnerText (element, innerText) {
if (element.hasChildNodes) {
while (element.hasChildNodes()) {
element.removeChild(element.lastChild);
}
var textNode;
if (document.createTextNode &&
(textNode = document.createTextNode(innerText)))
{
element.appendChild(textNode);
}
}
}

setInnerText(player.item(player.length - 1), av);

But I don't have a Mac here to test what the problem is and whether the
solution above solves that, so try yourself and report back to the group
whether it works.
 
M

Mick White

Martin said:
Mick White wrote:



function setInnerText (element, innerText) {
if (element.hasChildNodes) {
while (element.hasChildNodes()) {
element.removeChild(element.lastChild);
}
var textNode;
if (document.createTextNode &&
(textNode = document.createTextNode(innerText)))
{
element.appendChild(textNode);
}
}
}

setInnerText(player.item(player.length - 1), av);

But I don't have a Mac here to test what the problem is and whether the
solution above solves that, so try yourself and report back to the group
whether it works.

Works like a charm, Martin. (Camino 0.8, NN7, Safari 1.0.2, Mozilla 1.6,
Firefox 0.9, MS IE5.2 [all mac])
Thanks.
Mick
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top