Problem dynamically writing HTML with W3C DOM

E

ezmiller

Hi,
I am trying to learn how to dynamically write HTML using the W3C DOM.
But I am having trouble with the code. It doesn't seem to be doing
what I expect it to and I am not sure why.

Let me give an example.

document.getElementsByTagName("head")[0].appendChild(document.createElement("title"));
alert(document.getElementsByTagName("title")[0].tagName);

document.getElementsByTagName("title")[0].appendChild(createTextNode("Assignment
6"));

This code produces an error ("Object expected") on the 3rd line. I am
sure that the <title> tag has been created because the second line
produces a message box with "TITLE" in it.

Any ideas what's going wrong??
 
C

Christopher Benson-Manica

ezmiller said:
document.getElementsByTagName("head")[0].appendChild(document.createElement("title"));
alert(document.getElementsByTagName("title")[0].tagName);
document.getElementsByTagName("title")[0].appendChild(createTextNode("Assignment
6"));

Any ideas what's going wrong??

It may not fix all your troubles, but createTextNode() is a method of
the document object.

document.getElementsByTagName("title")[0].appendChild(document.createTextNode("Assignment 6"));
^^^^^^^^
 
E

ezmiller

Right, I caught that just after I posted, but see my new message, where
after I fix that I get an error message: "Unexpected call to method or
property access."...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top