First, my HTML:
<p id="p"></p>
<input id="input" />
<button onclick="fill()">Fill</button>
Then my Javascript:
function fill() {
var x = document.getElementById('input').value;
var y = x.split('');
for (var i = 0; i < y.length; i++) {
if (i == 0) {
setTimeout(function() {
document.getElementById('p').innerHTML = y[i];
},(i * 50));
}
setTimeout(function() {
document.getElementById('p').innerHTML += y[i];
},(i * 50));
}
}
What it does is take text from a textfield, cut each character including spaces into an array, then loop through the array, displaying each character at 50ms intervals. The effect is supposed to look like it is typing itself out.
The effect works fine, but the values of the array don't seem to be. If I were to type "abc123" then I would expect that to come right back out, but instead I get:
undefinedundefinedundefinedundefinedundefinedundefined
Using console.log()
, when I check the array it looks fine, when I check the typeof
of the individual array values I get string
, but when I check the typeof
for the array, I get object
. Maybe this is what's wrecking it...
I have tried use toString()
on the y[i]
which just spits out "[object Window]", I have tried defining the array like this var y = new Array;
and then doing the split()
, nothing works. I am at a complete loss. Really would love some help here. Thanks!