Question:
Dynamically adding text to a div tag using Javascript???
Robmaister12
2008-04-22 18:39:05 UTC
Ok, I have been working on a website for school and I am working on parsing an XML page into a table in an HTML page using Javascript. So far, I have been able to successfully parse the data onto the page. Now, for the past few days, I have been working on selecting a single group from a drop down list. The drop down list loads just fine with the proper text and it can distinguish one group but I am having some trouble getting the text back on the page. Right now it will blank the page and come up with a white background and it isn't aligned. I thought I could drop the code in to a div tag, but I am having some trouble. I am parsing the data and using "document.write()" to display it. I want everything that is written by document.write to be displayed in a div tag so the data doesn't come up in a new page.





Three answers:
richarduie
2008-04-22 23:05:43 UTC
Avoid document.write(); it's destructive of other content unless managed carefully. Try either DOM operations such as appendChild() or direct manipulation of the innerHTML property of the target container div. In the following example, get() and create*() methods are just shorthand names to let the lines display in YA. Replace "break" with "br" in the catenate function.















original, base content goes here





xml will be added below here







gillette
2016-10-18 01:28:50 UTC
Javascript Add Text
Jim H
2008-04-22 18:44:52 UTC
Include the
and
tags in the document.write() statement.



For example. the variable x has the HTML you want to write:

document.write('
'+x+'
')


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...