insert element in DOM

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

    #1

    insert element in DOM

    I'm trying out a script called mktree.js
    < http://www.mattkruse.com/javascript/mktree/ >
    I want to add an expand all button to the script, using the DOM
    insertBefore method. I added a function to the mktree.js script, but
    it failed to insert the button I created. However, Firefox's javscript
    console did not report an error. AFAICS, it didn't find the element
    with id="root".

    I made a second page, this time putting the script to insert the
    button inside the page itself, and it worked. I'm confused why the
    funtion fails in the external js file, not inside the html document.

    The test cases can be found here:

    < http://www.tsmchughs.com/test/insert/ >

    --
    Brian (remove ".invalid" to email me)

  • Ivo

    #2
    Re: insert element in DOM

    "Brian" wrote[color=blue]
    > insertBefore method. I added a function to the mktree.js script, but
    > it failed to insert the button I created. However, Firefox's javscript
    > console did not report an error. AFAICS, it didn't find the element
    > with id="root".
    >
    > I made a second page, this time putting the script to insert the
    > button inside the page itself, and it worked. I'm confused why the
    > funtion fails in the external js file, not inside the html document.
    >
    > The test cases can be found here:
    >
    > < http://www.tsmchughs.com/test/insert/ >[/color]

    Simple. In your external file the code responsible for adding the button is
    called before the page renders, before there is an element with id=root. Run
    it onload and you should be fine. In the internal version, the button code
    is placed at the end, when the element is there already.
    Also,
    newButton = document.create Element("BUTTON ");
    newButton.setAt tribute("type", "button");
    seems odd. What other type can a button be?
    HTH
    Ivo


    Comment

    • Brian

      #3
      Re: insert element in DOM

      Ivo wrote:[color=blue]
      > "Brian" wrote
      >[color=green]
      >> insertBefore method. I added a function to the mktree.js script,
      >> but it failed to insert the button I created.
      >>
      >> The test cases can be found here:
      >>
      >> < http://www.tsmchughs.com/test/insert/ >[/color]
      >
      > Simple. In your external file the code responsible for adding the
      > button is called before the page renders, before there is an
      > element with id=root. Run it onload and you should be fine.[/color]

      Gawd I can be such a dolt sometimes. The script adds the main function
      to window.onload, but the mkButtons script was run as is. I added it
      to onload, and works fine now. If you knew how long I tore my hair
      out over this. :-/
      [color=blue]
      > newButton = document.create Element("BUTTON ");
      > newButton.setAt tribute("type", "button");[/color]
      [color=blue]
      > seems odd. What other type can a button be?[/color]

      submit (the default) and reset

      http://www.w3.org/TR/html4/interact/...ml#edef-BUTTON

      --
      Brian (remove ".invalid" to email me)

      Comment

      Working...