function $(selector) {
var resultObject = {
append: function (element) {
var parser = new DOMParser();
var dos = parser.parseFromString(element, "text/html");
var all = dos.getElementsByTagName("body")[0];
var elemWhichAppend = document.getElementsByClassName("testing");
var children = all.childNodes;
for (var i = 0; i < elemWhichAppend.length; i++) {
var msgContainer = document.createDocumentFragment();
var children = all.childNodes;
for (var child = 0; child < children.length; child++) {
var al = myLo(children[child]);
msgContainer.appendChild(al);
}
insertAfter(msgContainer, elemWhichAppend[i]);
}
}
}
return resultObject;
}
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
function myLo(curElem)
{
var whichnodetype;
if (curElem.nodeType == 3)
{
whichnodetype = document.createTextNode(curElem.nodeValue);
}
else
{
var whichnodetype = document.createElement(curElem.nodeName);
}
var children = curElem.childNodes;
for (var child = 0; child < children.length; child++) {
whichnodetype.appendChild(children[child]);
}
return whichnodetype;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<h1 class="testing">APPEND</h1>
<p>Hallo, ich bin ein P TAG </p>
<h2 class="testing">APPEND2</h2>
<input type="button" value="append tag/text " onclick="$('.testing').append('<ul><li>RIBA RIBI<ul><li>FRANK RIBERY</li></ul></li></ul> <h1>NIGOGOG</h1> messi ist scheiße');" />
</body>
</html>
I managed to add every element to the first tag, but at the second time it only adds the last tag. See code snipped. It goes two times each loop which is correct but it still adds not correct the second time.
I am quite puzzled by how you have gone about this, and why you have replaced
append
with your own version, but as an example of how to append to multiple targets using jQuery in a fraction of the code:JSFiddle: https://jsfiddle.net/TrueBlueAussie/ad2qf4uy/
If you could please explain what your aim was, that would make life so much easier :)