468,545 Members | 1,873 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,545 developers. It's quick & easy.

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)
http://www.tsmchughs.com/
Jul 23 '05 #1
2 2163
Ivo
"Brian" wrote
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/ >


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.createElement("BUTTON");
newButton.setAttribute("type", "button");
seems odd. What other type can a button be?
HTH
Ivo
Jul 23 '05 #2
Ivo wrote:
"Brian" wrote
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/ >
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.


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. :-/
newButton = document.createElement("BUTTON");
newButton.setAttribute("type", "button"); seems odd. What other type can a button be?


submit (the default) and reset

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

--
Brian (remove ".invalid" to email me)
http://www.tsmchughs.com/
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andrew | last post: by
28 posts views Thread by Giggle Girl | last post: by
2 posts views Thread by mirandacascade | last post: by
4 posts views Thread by Tim Slattery | last post: by
1 post views Thread by subramanian100in | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.