I need to append multiple nodes to a container. Rather than doing a slow DOM append inside each iteration, I want to queue up the nodes in a document fragment (open to other ideas) and append them all at one time. Here is my code:
var fragment = document.createDocumentFragment();
$.each( poFailureInfoMultiple, function(i,e){
fragment.appendChild(
$('<button/>', {
'class': 'el-contents-center multiple-record'
})
);
});
$('#some-container').html( fragment );
My problem is I am getting an error message stating:
Could not convert JavaScript argument arg 0 [nsIDOMDocumentFragment.appendChild]
So how can I append multiple element nodes to my DOM at once? I don’t HAVE to use the fragment method (I just found it and it seemed viable).
Note:
I DO NOT WANT TO USE HTML SYNTAX FOR THE APPEND
i.e. $('#some-container').append('<button class="myclass"></button>');
The add method on a jQuery object doesn’t alter the object itself but returns a new object, which is why you need
elemsToAppend = elemsToAppend.add(...). I honestly cant say how fast this method is though. I actually think the html way is faster though.