I started with a string:
hidden = "a,b,c,d,e,f,g,h";
I then split(",") the string into something I can try to work with:
array = hidden.split(",");
If I console.log out, the result is:
["a", "b", "c", "d", "e", "f", "g", "h"]
What I would eventually like to end up with (using JS only) is:
["a,b,c,d","e,f,g,h"];
or
[["a,b,c,d"],["e,f,g,h"]];
Thanks!
EDIT to show that I am actually trying to figure this out (don't mind the formatting):
if (currentPage === "view-bills.php")
{
var str = "";
hidden = document.getElementById("billItems");
array = hidden.value.split(",");
for (var i=0;i<array.length;i++)
{
str += "<option>"+ array[i] +"</option>";
}
var itemsAmt = document.getElementById("items").value;
document.getElementById("displayItems").innerHTML = "<optgroup label='"+ itemsAmt +" items'>" + str + "</optgroup>";
}
UPDATE:
The problem has been solved, however, I would like to show you the outcome of my project (accessing classes instead of id's this time):
http://i.imgur.com/umjhGPP.png
Once again, don't mind my formatting, I have trouble pasting things in here:
if (currentPage === "view-bills.php")
{
// Add items to select box
hidden = document.getElementsByClassName("billItems");
for (var n=0;n<hidden.length;n++)
{
array = hidden[n].value.split(",");
groups = [];
for(var i=0;i<array.length;i+=5)
{
groups.push(array.slice(i,i+5).join(','))
}
var str = "";
for (var j=0;j<groups.length;j++)
{
str += "<option>"+ groups[j] +"</option>";
}
var itemsAmt = document.getElementsByClassName("items")[n].value;
document.getElementsByClassName("displayItems")[n].innerHTML = "<optgroup label='"+ itemsAmt +" items'>" + str + "</optgroup>";
}
}