var bod = document.getElementsByTagName(body);
bod.innerHTML = "Hi";
4 Answers
1-> you have to provide a tagName as a string
2-> you have to pick the first element of the returned (nodeList) collection
var bod = document.getElementsByTagName('body')[0];
bod.innerHTML = "Hi";
1 Comment
KooiInc
Noticed strange behavior of the SO-editor here: after a numbered list formatting code doesn't work anymore, hence ->.
You are missing quotes:
var bod = document.getElementsByTagName(body);
----------------------------------------^
Try this with addition of [0] at the end to get the body itself rather than NodeList return by getElementsByTagName:
var bod = document.getElementsByTagName('body')[0];
bod.innerHTML = "Hi";
Or you can go for this shorter version:
var bod = document.body;
bod.innerHTML = "Hi";
Or even shorter:
document.body.innerHTML = "Hi";
1 Comment
Nick Craver
To be clear, that's not an array, it's like an array, it's actually a
NodeList.
NodeList, not an array :)