This question refers to one of mine about split(), and include an answered question about inserting specific thing if string number is x.
Basically, I have one big string, with a specific separator which is :
, and I'm using .split()
in a loop to print each part of the string into a different input using the separator to cut it. This works perfectly.
Now, what I want is to do a breakline after each 17 inputs (input 17 then linebreak, input 34 then linebreak, input 51...). I found a way to do it in the second link I shared, and the breakline works. The problem is that only the last line of inputs is now filled with the string parts (the last 7 parts of the string). All the other input are simply blank.
Here's the final code:
function cutTheString()
{
var str = document.getElementById('textareaString').value;
var arrayOfStrings = str.split(':');
for(var i = 0; i < arrayOfStrings.length; i++)
{
if ((i % 17) == 0)
{
document.getElementById('result').innerHTML += '<br />';
}
var div = document.getElementById('result');
var mi = document.createElement('input');
mi.setAttribute('type', 'text');
mi.setAttribute('size', '4');
mi.setAttribute('id', 'string' + (i+1));
div.appendChild(mi);
document.getElementById('string' + (i+1)).value = arrayOfStrings[i];
}
}
Interestingly, in if ((i % 17) == 0)
, let's replace 0
by x
. If x < 17
, only x - 17
inputs will be filled, starting by the last one of the list. If x > 17
, all inputs will be filled, but the breaklines will obviously not work.
There is no error in the console, and my knowledge of Javascript makes me fail to understand why it's doing this. I quite see the correlation, but I can't think of any solution. I tried different operators than ==
without success.
You can test it on this JSFiddle. Thanks for your help!