write html content from javascript

Posted by Nikita Rybak on Stack Overflow See other posts from Stack Overflow or by Nikita Rybak
Published on 2010-06-09T21:11:57Z Indexed on 2010/06/09 21:22 UTC
Read the original article Hit count: 328

Filed under:
|

There's one thing I want to do with javascript, but don't know how. In a perfect world it would look like this:

<p>My very cool page!</p>
<script type="text/javascript">
    document.write('<div>some content</div>');
</script>

And this script would insert <div>some content</div> right before (or after, or instead of) script tag. But in the real world, document.write starts writing html anew, removing any static content in the page (<p> tag, in this case).

This is simplified example, but should give you the idea. I know that I can statically put <div id="some_id"></div> before script tag and insert html in it from js, but I wanna be able to use multiple instances of this snippet without changing it (generating random id manually) each time.

I'm ok to use jquery or any other existing library as well. Is there any way to achieve this? Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html