Connecting Tech Pros Worldwide Help | Site Map

Problem dynamically writing HTML with W3C DOM

ezmiller
Guest
 
Posts: n/a
#1: Dec 6 '05
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??

Christopher Benson-Manica
Guest
 
Posts: n/a
#2: Dec 6 '05

re: Problem dynamically writing HTML with W3C DOM


ezmiller <ethanzanemiller@gmail.com> wrote:
[color=blue]
> document.getElementsByTagName("head")[0].appendChild(document.createElement("title"));
> alert(document.getElementsByTagName("title")[0].tagName);[/color]
[color=blue]
> document.getElementsByTagName("title")[0].appendChild(createTextNode("Assignment
> 6"));[/color]
[color=blue]
> Any ideas what's going wrong??[/color]

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"));
^^^^^^^^

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
ezmiller
Guest
 
Posts: n/a
#3: Dec 6 '05

re: Problem dynamically writing HTML with W3C DOM


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."...

Closed Thread