David said:
Hello All,
I am trying to split a long list of list items ( li ) into 3 groups and
append them to a div on the page. Below is the test page that I have
created. It's fairly simple but I can't seem to get my head wrapped
around
it. Anyone here can offer a boost?
http://mysite.verizon.net/microweb2/3collist.html
In the line:
var container = document.getElementByID('writeMe');
You have the wrong capitalisation for getElementById()
I think what you are trying to do is divide the li elements evenly
between 3 ul elements. If so, you need to work out how many to put
into each ul, then do it. Rather than try to munge the script you have,
I'll just post the following script give you some pointers:
function quickTest()
{
var numSections = 3; // Number of sections
var numPerSection; // Number in each section (calculate)
var extras = 0; // Remainder if not equal number
// in all sections
var oUL; // New ul element
var div = document.getElementById('pContent');
var ul = div.getElementsByTagName('ul')[0];
var lis = ul.getElementsByTagName('li');
var numPerSection = lis.length/numSections | 0;
if (numPerSection) {
extras = lis.length % numSections;
} else {
numPerSection = lis.length;
}
for (var i=0; i<numSections; i++){
oUL = document.createElement('ul');
for (var j=0; j<numPerSection; j++){
oUL.appendChild(lis[0]);
}
if (extras-- > 0){
oUL.appendChild(lis[0]);
}
div.appendChild(oUL);
}
div.removeChild(ul);
}