I’ve written a script that slices a mass of text into 1000 character blocks and groups them in an object. Now what I’m trying to do is take all the text that is put in the textarea #PATypes, slice it up into 1000 character blocks, then put each individual block into a cell in a table. Unfortunately, I’m running into trouble- does anyone have an idea what I am doing wrong?
I’ve written the relevant snippets down below; if you’d like to see the whole thing, check here:
http://jsfiddle.net/ayoformayo/yTcRb/1/
HTML
<label for="PATypes">Mass Text PA</label>
<textarea rows="2" cols="20" id="PATypes"></textarea>
<button onclick = "cutUp()">Submit PA</button>
<table>
<tr>
<td id="PAType=Name=Value1"></td>
<td id="PAType=Name=Value2"></td>
<td id="PAType=Name=Value3"></td>
<td id="PAType=Name=Value4"></td>
<td id="PAType=Name=Value5"></td>
<td id="PAType=Name=Value6"></td>
</tr>
</table
Javascript
function cutUp(){
var chunks = [];
var my_long_string = document.getElementById('PATypes').value;
var i = 0;
var n = 0;
while(n < my_long_string.length) {
n = 1000 * i;
chunks.push(my_long_string.slice(n, n + 1000));
i++;
}
document.getElementById('PAType=Name=Value4').innerHTML = chunks[0];
document.getElementById('PAType=Name=Value5').innerHTML = chunks[1];
document.getElementById('PAType=Name=Value6').innerHTML = chunks[2];
}
One problem is that you’ve defined your
cutUpfunction in anonloadhandler in the jsFiddle. You need to select a no wrap option from the menu on the left.Another issue is that your
chunksarray will always have an empty string at the end. You can fix that by incrementingnbefore the next evaluation of thewhile.DEMO: http://jsfiddle.net/UFTjJ/
Or if you don’t like the placement of the assignment, do it after…