Problem dynamically writing HTML with W3C DOM

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ezmiller

    Problem dynamically writing HTML with W3C DOM

    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.getEle mentsByTagName( "head")[0].appendChild(do cument.createEl ement("title")) ;
    alert(document. getElementsByTa gName("title")[0].tagName);

    document.getEle mentsByTagName( "title")[0].appendChild(cr eateTextNode("A ssignment
    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

    #2
    Re: Problem dynamically writing HTML with W3C DOM

    ezmiller <ethanzanemille r@gmail.com> wrote:
    [color=blue]
    > document.getEle mentsByTagName( "head")[0].appendChild(do cument.createEl ement("title")) ;
    > alert(document. getElementsByTa gName("title")[0].tagName);[/color]
    [color=blue]
    > document.getEle mentsByTagName( "title")[0].appendChild(cr eateTextNode("A ssignment
    > 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.getEle mentsByTagName( "title")[0].appendChild(do cument.createTe xtNode("Assignm ent 6"));
    ^^^^^^^^

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

    Comment

    • ezmiller

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

      Comment

      Working...