Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot append DOM element to DIV node: Uncaught HierarchyRequestError: Failed to execute 'appendChild' on 'Node'

Using the DOM parser I parsed a string , and then tried to append the object to a container, like so:

      var newCategory = 
        "<div class=\"column-expenses-left\" id=\"newCategory"+ expenseCategoryCssName +"\">" +
          "<hr />" +
          "<div style=\"text-align: center;\">" +
            "<?php echo $budgetExpense[\'ExpenseCategory\'][\'cssName\']; ?>&nbsp;" +
            "<span>" +
            "<a href=\"#\" class=\"delete-category\"><img alt=\"\" src=\"/theme/all/img/Trash_can_small.png\" style=\"width: 15px; height: 15px; float: right;\" id=\"\" alt=\"Delete\"/></a>"+
            "</span>" +
          "</div>" +
          "<hr />" +
          "<p class=\"pointer\" style=\"text-align: center;\"><img alt=\"\" src=\"/theme/all/img/add_to_yours.png\" style=\"display: inline-block;\" /></p>" +
        "</div> <!-- column-expenses-left -->";

      // get the object to append to 
      var stack = document.getElementById('newCategories');

      var parser = new DOMParser();
      var newNode = parser.parseFromString(newCategory, "text/xml");

      stack.appendChild(newNode);

However I get the following error:

 Uncaught HierarchyRequestError: Failed to execute 'appendChild' on 'Node': Nodes of type '#document' may not be inserted inside nodes of type 'DIV'.

I don't quite understand what is happening here? Is there a way to use the parser so that it creates a node of type DIV instead? Is this even a good idea?

like image 257
user1658296 Avatar asked Apr 15 '15 06:04

user1658296


People also ask

Why appendChild is not working on node?

question: failed to execute ‘appendChild’ on ‘node’: parameter 1 is not of type ‘node’ reason: the parameter of appendChild is node. This problem indicates that the current parameter is not node, it may be a string If the added element is a string, use document.

Why can't I append a node to a Div?

It's actually a simple answer. Your function is returning a string rather than the div node. appendChild can only append a node. The above code will never work. What will work is: This can happen if you accidentally are not dragging the element that does have an id assigned (for example you are dragging the surrounding element).

What is appendChild method in HTML DOM?

HTML DOM appendChild () Method 1 Definition and Usage. The appendChild () method appends a node as the last child of a node. ... 2 Browser Support 3 Syntax 4 Parameter Values 5 Technical Details 6 More Examples

How to append a node as the last child of a node?

The appendChild () method appends a node as the last child of a node. Tip: If you want to create a new paragraph, with text, remember to create the text as a Text node which you append to the paragraph, then append the paragraph to the document. You can also use this method to move an element from one element to another...


1 Answers

You can't append document nodes (the result of parseFromString). Instead take the child of the document object and append it:

var parser = new DOMParser();
var newNode = parser.parseFromString(newCategory, "text/xml");
stack.appendChild(newNode.documentElement);

Note, that your HTML is not XML-compliant so you might get errors parsing it. In this case make sure you fix them (like duplicated alt attribute, &nbsp; entities).

However, in your case I doubt you need to parse XML at all. You can just append entire HTML string in the first place. For example like this:

var stack = document.getElementById('newCategories');
stack.insertAdjacentHTML('beforeend', newCategory);
like image 128
dfsq Avatar answered Sep 23 '22 19:09

dfsq