javascript - Cannot append DOM element to DIV node: Uncaught HierarchyRequestError: Failed to execute 'appendChild' on 'Node' -
using dom parser parsed string , , tried append object container, so:
var newcategory = "<div class=\"column-expenses-left\" id=\"newcategory"+ expensecategorycssname +"\">" + "<hr />" + "<div style=\"text-align: center;\">" + "<?php echo $budgetexpense[\'expensecategory\'][\'cssname\']; ?> " + "<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 -->"; // object append var stack = document.getelementbyid('newcategories'); var parser = new domparser(); var newnode = parser.parsefromstring(newcategory, "text/xml"); stack.appendchild(newnode);
however following error:
uncaught hierarchyrequesterror: failed execute 'appendchild' on 'node': nodes of type '#document' may not inserted inside nodes of type 'div'.
i don't quite understand happening here? there way use parser creates node of type div instead? idea?
you can't append document nodes (the result of parsefromstring
). instead take child of document object , append it:
var parser = new domparser(); var newnode = parser.parsefromstring(newcategory, "text/xml"); stack.appendchild(newnode.documentelement);
note, html not xml-complient might errors parsing it. in case make sure fix them (like duplicated alt
attribute,
entities).
however, in case doubt need parse xml @ all. can append entire html string in first place. example this:
var stack = document.getelementbyid('newcategories'); stack.insertadjacenthtml('beforeend', newcategory);
Comments
Post a Comment