There is a list of HTML elements with a common parent:
<div> <div>...</div> <div>...</div> <div>...</div> </div> I get the following items using an AJAX request; they come as text:
<div>...</div><div>...</div><div>...</div> How best (and why) to add them to the end of the current list :
<div> <div>...</div> <div>...</div> <div>...</div> <div>...</div><!-- Новый пункт --> <div>...</div><!-- Новый пункт --> <div>...</div><!-- Новый пункт --> </div> 1. Take the innerHTML parent (that is, the already existing clauses), add the result of the AJAX request to them and shove it all into the parent:
parentOfItems.innerHTML = parentOfItems.innerHTML + responseText; The first option seemed to work fine, but when I next used it on some project, where a bunch of some handlers were hung on the parent of the list, there were defects; so I began to look for alternative ways to implement this idea; from this, in fact, this question arose.
2. Create a lowercase ( span ) wrapper, put the result of an AJAX request into its innerHTML and add this wrapper to the parent:
var wrapper = document.createElement('span'); wrapper.innerHTML = responseText; parentOfItems.appendChild( wrapper ); That is, the result will be the following:
<div> <div>...</div> <div>...</div> <div>...</div> <span> <div>...</div><!-- Новый пункт --> <div>...</div><!-- Новый пункт --> <div>...</div><!-- Новый пункт --> </span> </div> 3. Create a temporary wrapper, put the result of the AJAX request into its innerHTML and add the children of this wrapper to the parent of the list items:
var tmpWrapper = document.createElement('div'); tmpWrapper.innerHTML = responseText; for ( var i = 0; i < tmpWrapper.children.length; i++ ) { parentOfItems.appendChild( tmpWrapper.children[i] ); } 4. Another option has just occurred: use JSON; that is, convert the exhaust on the server to JSON, and on the client, unpack it and simply loop through the items, adding them to the parent. However, this option seems to me the most clumsy.
responseText- corrected the question. - Roman Grinyov