PDA

View Full Version : insert a new element inline using javascript at any place


jayapalchandran
06-19-2009, 11:47 PM
Hi,
how to insert elements inline using javascript at any place in a html document
look at the code so that you will understand my query.

<body>
<div> ... </div>
...
...
...
<script>
var tm = document.createElement("div")
tm.style.border = "1px solid gray"
tm.style.paddig = "5px"
tm.innerHTML = "testing"
document.write(tm)
</script>
...
...
...
</body>

the above javascript code will print HTMLDivElement or something similar to this...

how to insert elements inline using javascript at any place in a html document?

abduraooft
06-20-2009, 09:41 AM
Use the proper DOM method appendChild() to add a child element to a selected element.
btw, you may not be able to execute such functions from the <head> tag before completely loading your document. You may enclose your statements inside a function and invoke that function when from the onload event of your window.

jayapalchandran
06-23-2009, 08:21 PM
hi,
still now i am using appendchild. if you see the code clearly i have written the script tab after a div statement and not in the head section. i have seen codes which gets inserted at anyplace so i thought there might be a way instead of getting the reference of a parent element to insert the child in it...