javascript - function to mirror insertAdjacentHTML -


i trying write function mirror insertadjacenthtml dom method element.insertadjacenthtml , here is

function insertadjacent(targetelement) { 'use strict'; return {     insertafter: function (newelement, targetelement) {         var parent = targetelement.parentnode;         if (parent.lastchild === targetelement) {             parent.appendchild(newelement);         } else {             parent.insertbefore(newelement, targetelement.nextsibling);         }     },      insertatbegin: function (newelement) {         var fchild = targetelement.firstchild;         if (!fchild) {             targetelement.appendchild(newelement);         } else {             targetelement.insertbefore(newelement, fchild);         }     },      insertatend: function (newelement) {         var lchild = targetelement.lastchild;         if (!lchild) {             targetelement.appendchild(newelement);         } else {             this.insertafter(newelement, targetelement.lastchild);         }     } }; } 

the function works fine when insert 2 different element nodes @ beginning , end shown here. problem comes when try insert same element node @ beginning , end shown here. inserts element node @ end , not @ both beginning , end. causing issue? thank you

because 1 element can't insert in 2 place @ same time, if want it, @ each function's first line, add newelement = newelement.clonenode(true); i've altered 2nd jsfiddle, have look.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -