I hope you can get me out of my current predicament which involves coding style when it comes to appending many elements in a pretty flexible and typesafe way.
Now I see my code below and I feel that it can be written in a more compact and shorter manner, especially in my set function. Also, I'm not sure if I should merge the arrays and keep it in one line.
What makes me wonder most however, is how to append all those divs to a document fragment before placing them to the real DOM as it is fairly complex.
Would you say I'm right about the possible improvements that can be made? If so, then how can I improve it?
var superContainer = document.getElementsByClassName('superContainer');
var article = document.getElementsByTagName('article');
var obj = {};
var wrappers = ['a', 'b', 'c'];
var a1 = ['fruits', 'meat', 'milk', 'soup', 'sweets'];
var a2 = ['troll', 'gollum', 'imp', 'ork', 'dolly'];
var a3 = ['foobar'];
var misc = ['boring', 'fun', 'annoying', 'great'];
function Constructor() {
function set() {
for(var i = 0; i < article.length; i++) {
var tempSC = document.createElement('div');
tempSC.appendChild(article[i].parentNode.replaceChild(tempSC, article[i]));
tempSC.className = 'superContainer';
create(superContainer[i], wrappers, 0, 0);
create(obj.a[i], a1, 0, 0);
create(obj.b[i], a2, 0, 0);
create(obj.c[i], a3, 0, 0);
create(obj.milk[i], misc, 1, 2);
create(obj.sweets[i], misc, 0, 1);
create(obj.milk[i].children[0], misc, 3, 0);
create(obj.sweets[i].children[1], misc, 3, 0);
}
}
set();
function create(parent, elem, loopStart, loopEnd) {
for(var i = loopStart; i < elem.length - loopEnd; i++) {
var result = document.createElement('div');
result.className = elem[i];
parent.appendChild(result);
obj[elem[i]] = document.getElementsByClassName(elem[i]);
}
}
}
Constructor();
}