“needs to be divided into multiple array after 5 comma seperated values”
You seem to be saying that the result should be one array with five values in it and then a second array with the next five values in it, etc. If so, you can do the following:
var list = "<%=ab%>";
list = list.slice(1,-1); // remove the enclosing []
var allValues = list.split(/\s*,\s*/); // split on comma with optional whitespace
var a = [];
for (var i = 0; i < allValues.length; i+=5) {
a.push( allValues.slice(i, i+5<=allValues.length ? i+5 : allValues.length) );
}
After running the above, a
will be array that contains other arrays. a[0]
is an array of the first five items, so, e.g., a[0][2]
is the third item. a[1]
is an array of the next five items. If there were more than ten items in the original list then a[2]
would be an array of the next five, etc. If the total number of items is not divisible by five then the last array would hold the remainder.
Demo: http://jsfiddle.net/9xAxz/
“Then I need to put each array values to textbox values”
Um… does that mean one item per textbox? If so, why did you want to divide up after five values? Do you mean five items per textbox? Either way are you talking about dynamically creating textboxes? If so you can follow the above code with something like this:
var tb;
for (i = 0; i < a.length; i++) {
tb = document.createElement('input');
tb.value = a[i].join(", ");
document.body.appendChild(tb);
}
Demo: http://jsfiddle.net/9xAxz/1/
If I’m completely off base on any of the above, well… think about editing your question to make it clearer. I suppose I should’ve clarified before answering, but please update the question to show what your desired output is for that input.
2
solved String to arrays