1

I was wondering if it was possible to get Javascript to write some HTML onto the page in a certain DIV.

This is due to the fact, there are certain areas of the site where i don't have access to the markup. But i would like to add a small section there.

For example the container i want to add some html to is <div id="topics"></div>

Is it possible to get Javascript to do this <*div id="topics"> <div id="mysection"> </div> <*/div>

Many thanks!

asked Jun 7, 2009 at 16:03

5 Answers 5

6

This is fairly simple to do, even using plain JavaScript.

var topicsDiv = document.getElementById("topics");
topicsDiv.innerHTML = '<div id="mysection"> </div>';

If you're going to be doing some serious DOM (Document Object Model, i.e. HTML structure) manipulation, however, then I would recommend you look into using the JQuery library. Yet if the task is limited to your question, then normal JavaScript should be fine, as shown above.

answered Jun 7, 2009 at 16:06
Sign up to request clarification or add additional context in comments.

2 Comments

innerHTML has "HTML" capitalised, and don't forget to escape your double quotes!
@Perspx: Or just use single quotes. :)
5

With simple Javascript (without JQuery or something you could do):

HTML:

<div id="topics"></div>

JS:

document.getElementById('topics').innerHTML = '<div id="mysection"></div>';

Using JQuery you would simply do:

$('#topics').append('<div id="mysection"></div>');
Pim Jager
32.2k17 gold badges75 silver badges100 bronze badges
answered Jun 7, 2009 at 16:08

Comments

1

Of course. For example, you can do this using Prototype:

$('topics').update('<div id="mysection"></div>');

The syntax is quite similar for jQuery or another frameworks, and as Noldorin noted, you can do also this without any framework.

answered Jun 7, 2009 at 16:08

1 Comment

Bump on the use of Prototype.
1

You are probably looking for the innerHTML property

document.getElementById('topics').innerHTML = 'whatever';
Nosredna
86.8k16 gold badges98 silver badges124 bronze badges
answered Jun 7, 2009 at 16:09

2 Comments

Just to be pedantic, innerHTML is a property, not a function.
I took the liberty of making that fix.
0

I agree with both Noldorin and Can Berk Güder and others, and I'd like to quote that this is DOM (Document Object Model) and one of the main components of AJAX.
AJAX send a request to a server, and uses techniques such as this to "put it" in the page.

Know that you can do almost anything with javascript; you could just have "<html><body></body></html>" and have javascript do ALL the rest. This is what GWT does, and if you need to HEAVILY modify you page dynamically, you may be interested in it.

answered Jun 7, 2009 at 16:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.