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

6
  • What's going on with those "getElementsByTagName()" calls, where the tag names are "url" and "children"? What sort of content is that? What is "element"? Commented Jul 18, 2011 at 18:19
  • @Pointy: the tag names are from an XML file which is phrased (with JS), here ( 4shared.com/file/5kD6WzWr/Tree.html ) are the zipped files Commented Jul 18, 2011 at 20:20
  • OK so what does "element" refer to? If it's an HTML DOM node, then the reason Firefox complains because it's not letting you set the "children" attribute in the first place; that's a built-in read-only attribute of all HTML element nodes. Commented Jul 18, 2011 at 20:26
  • @Pointy: Any cross-browser solution here? Commented Jul 18, 2011 at 20:37
  • Well it's hard to say because it's unclear what you're doing. Your question title is about "JavaScript objects", but it looks like you're actually trying to build the DOM. If that's what you want to do, then you have to use the DOM APIs as @citizen conn described in that answer. Commented Jul 18, 2011 at 20:40

1 Answer 1

1
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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.