I'm trying to create spans and append split string as characters within each span. So, given the message below, it'd be something like <span>t</span><span>h</span>
etc...
For some reason, I'm getting this error when I append to document body. Why?
Uncaught TypeError: Cannot read property 'appendChild' of null
<!doctype html>
<html>
<head>
<script type="text/javascript">
scramble();
function scramble () {
var message = "this is a message";
for (var i = 0; i < message.split("").length; i++) {
var letter = document.createElement('span');
letter.innerHTML = message.split("")[i];
document.body.appendChild(letter);
//document.getElementById("body").appendChild(letter);
}
}
function log (w) {
console.log(w);
}
</script>
</head>
<body>
</body>
</html>
I've also tried document.getElementById("body").appendChild(letter);
with the same error.
Stack trace:
Uncaught TypeError: Cannot read property 'appendChild' of null
myFunc
(anonymous function)