el.severo
el.severo

Reputation: 2287

Building JavaScript objects representing a tree

function createObjects(element, depth){
    element.label = element.getElementsByTagName("label")[0].firstChild.nodeValue;
    element.url = element.getElementsByTagName("url")[0].firstChild.nodeValue;
    element.depth = depth;
    if(treeWidths[depth] == undefined){
            treeWidths[depth] = 1;
    } else {
            treeWidths[depth]++;
    }

    element.children = new Array();
    allNodes.push(element);

    var children = element.getElementsByTagName("children")[0].childNodes;
    for(var i=0; i<children.length; i++){
            if(children[i].nodeType != 3 && children[i].tagName == "node"){
                    element.children.push(createObjects(children[i], depth+1));
            }
    }

    element.expanded = false;
    element.visible = false;
    element.moved = false;
    element.x = 0;
    element.y = 0;

    if (getNodeWidth() < element.label.length * 10)
            element.width = element.label.length * 10;
    else
            element.width = getNodeWidth();
    element.height = getNodeHeight();
    return element; }

Having problems with Firefox, it says that 'element.children.push' is not a function but works (only) in Google Chrome...

Any clue?

Upvotes: 1

Views: 822

Answers (1)

citizen conn
citizen conn

Reputation: 15390

element.children = new Array();

I wouldn't add an array as a property of an element. If you want to add children to an element, this is not the right way. Also if you want to just use an array to manage data, this is not the right way either.

If you want to add child elements use:

element.appendChild(createObjects(children[i], depth + 1));

Upvotes: 1

Related Questions