views:

58

answers:

5

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!

+1  A: 

Yes, but you always add or write it in relation to something else.

If you wanted to write something AFTER the first p tag, in jQuery, you could do something liek this

$('p:first').after( '<div>some content</div>' );

If you look at jQuery's API you will see they have many functions for this, such as before, after, append, etc.

Kerry
Sorry, that would limit number of code instances in the page to 1, or user would have to make up other identifier instead of 'p:first' each time.
Nikita Rybak
I gave you an example. You can put them all in a div like `<div id="content"></div>` then use `$('#content').append( '<div>some content</div>' );`
Kerry
'<div id="content">' is exactly the option I mentioned in my question. No need to repeat it.
Nikita Rybak
I don't see what your problem is? It's a solution? What do you want different? I don't understand how you're wording your question.
Kerry
'<div id' solution would require user to generate unique id each time he uses this code (possibly several examples on one page). It's not too difficult, but user may not be a programmer and I don't want to stretch his brain too much.
Nikita Rybak
I don't tunderstand how that's true, this isn't creating a new div id, it is simply appending content to a div id. No new div ids would have to be created.
Kerry
We can't have '<div id="content"></div>' twice, right? We have to use ids content1 and content2 and refer them $('#content1') from the first script instance and $('#content2') from the second.
Nikita Rybak
Instance of what? You don't create separate instances, you put them all within the same content, that id never changes. Any time you create it, you use the same div
Kerry
Instance of 'script' tag. The point was, if user puts two instances of script in different places in html, each of those places will show 'some content'.
Nikita Rybak
The user will be putting script tags in place? I didn't see that at all, I don't think it is possible to 'write in place'. It is much better if they have a button to create, than you can replace the button with the code you want
Kerry
A: 

You also might want to read about adding and removing elements to/from the DOM, such as in this article:

http://www.dustindiaz.com/add-remove-elements-reprise/

JYelton
Sorry, but doesn't seem to be relevant to the question at all.
Nikita Rybak
The question title is "Write html content from javascript" -- how is adding elements via Javascript *not* relevant?
JYelton
That's only title of the question. The question itself is about particular trick.
Nikita Rybak
Perhaps your question leaves some ambiguity that we need clarification on. You might want to explain *why* you need this functionality, to better help us answer you.
JYelton
In short, this piece of js queries another server and inserts returned html widget in the page. And each widget instance on the page is isolated (i.e., if you press button on one widget, it doesn't affect others). Not sure if it makes any difference.
Nikita Rybak
A: 

look up appendChild()

Kin
+2  A: 

We are actually in that perfect world...

and your example would work as it is ..

http://www.jsfiddle.net/BtKGV/

Gaby
That's interesting, probably it works because page is not yet loaded completely. I updated example to use button. But maybe I do not need to execute it after page is loaded, have to check. Thanks!
Nikita Rybak
@Nikita, i see ... indeed, you cannot use document write after the document has been closed (*completely loaded*).. the jquery `append()`, `prepend()` and `html()` will handle the adding before/after or replacing another element..
Gaby
A: 

To close the question, here's how it has worked out in the end.

The trick was to store each widget's data in html tag, not in javascript. User inserts content like this

<div class="my_widget" feed_id="123"></div>
...
<div class="my_widget" feed_id="456"></div>

User also links script from our server. When page's loaded, script does $('div.my_widget').each and populates contents of each widget depending on its feed_id and other attributes.

Thanks to everyone (and +1 to Gaby and Kerry for attempts to help with such vague problem)

Nikita Rybak