Search code examples
javascriptajaxxmljspdhtmlx

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.


I was using the script-let below to add a node to a dhtmlx tree. I was trying to get the id using getSelectedItemId() from a DHTMLX tree.And i encountered this error.

<script>
 var xmlDoc
 var xhttp = new XMLHttpRequest();
 xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       xmlDoc = this;
    }
};
xhttp.open("GET", "data/treeStruct.xml", true);
xhttp.send();

    var myTree;

function append(id){
                var x;  
                var ed1=document.getElementById('ed1').value;
                //alert(id);
                xmlDoc = xmlDoc.responseXML;
                var newEle = xmlDoc.createElement(id);
                newEle.appendChild(ed1);
                document.getElementsByTagName("tree")[0].appendChild(newEle);
            }
</script>           

Solution

  • DHTMLX Tree has its own API to add nodes. You need not interact with HTML directly. Just use code like next

    //to add a new item to the tree
    tree.insertNewChild(0,1,"tree")
    tree.insertNewChild(1,2,"child")
    //parent id, id, text
    

    Also, check the snippet http://snippet.dhtmlx.com/320fe781a