0
var bod = document.getElementsByTagName(body);
bod.innerHTML = "Hi";
4
  • 1
    getElementsByTagName returns an array of elements, not an element. Commented Dec 27, 2010 at 13:03
  • 1
    Try this document.body.innerHTML = "Hi"; Commented Dec 27, 2010 at 13:04
  • @Marc - It returns a NodeList, not an array :) Commented Dec 27, 2010 at 13:23
  • if you haven't already, check out the stack-exchange proposal here. Something you might find useful in the future. Commented Jan 14, 2011 at 8:07

4 Answers 4

4

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";
Sign up to request clarification or add additional context in comments.

1 Comment

Noticed strange behavior of the SO-editor here: after a numbered list formatting code doesn't work anymore, hence ->.
2

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

To be clear, that's not an array, it's like an array, it's actually a NodeList.
1

Because the getElementsByTagName() method returns a NodeList of all a elements with a specified name. In other word this return an List (something like array), so you need to get it first element in this case.

var bod = document.getElementsByTagName('body')[0];
bod.innerHTML = "Hi";

Comments

0
document.body.innerHTML = "Hi";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.