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 the textbox, strip every character, including spaces, into an array, and then loop through the array, displaying each character at 50ms spacing. The effect should look like it is printing itself.
The effect works great, but the array values ββdon't seem to look. If I were to type "abc123" I would expect it to return, but instead I get:
undefinedundefinedundefinedundefinedundefinedundefined
Using console.log()
when I check for an array it looks ok, when I check for typeof
individual values ββof the array I get string
, but when I check typeof
for an array I get object
. Perhaps this is what destroys it ...
I tried using toString()
on y[i]
, which just spits out "[object Window]", I tried to define an array like this var y = new Array;
and then doing it split()
, nothing works. I will completely lose. Actually I would like some kind of help. Thank!
javascript
string
object
arrays
Eric David Sartor
source
to share