Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Dynamically adding text to a div tag using Javascript???
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.
<div align="center">
<script type="text/javascript">
// Navyear is defined earlier.
populateAwards(navYear.options[navYear.selectedIndex].text);
</script>
</div>
Yeah, but I am trying to load it INTO the div tag. If I did put the div tag, then it would once again load the white background page, except the data would be centered.
By the way, it is not just 1 line of data I want to enter, It is a large table.
3 Answers
- richarduieLv 61 decade agoFavorite Answer
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.
<html>
<head>
<!--
No matter which one you select,
pick a Best Answer.
-->
<script type="text/javascript">
var xml = [
'xml content, line 1',
'xml content, line 2',
'xml content, line 3',
'xml content, line 4'
]
function get(eid) {
var d = document;
var r = d.getElementById(eid);
return r;
}
function createTN(t) {
var d = document;
var r = d.createTextNode(t);
return r;
}
function createEl(e) {
var d = document;
var r = d.createElement(e);
return r;
}
function appendXml() {
var x = get('xdiv');
var f = 'from append: ';
for (var i = 0; i < xml.length; i++) {
x.appendChild(createTN(f + xml[i]));
x.appendChild(createEl('BR'));
}
}
function catenateXml() {
var x = get('xdiv');
var f = 'from catenate: ';
for (var i = 0; i < xml.length; i++) {
x.innerHTML += f + xml[i] + '<break />';
}
}
function init() {
appendXml();
catenateXml();
}
window.onload = init;
</script>
</head>
<body>
<p>original, base content goes here</p>
<div id="xdiv">
<p>xml will be added below here</p>
</div>
</body>
</html>
- Jim HLv 51 decade ago
Include the <div> and </div> tags in the document.write() statement.
For example. the variable x has the HTML you want to write:
document.write('<div>'+x+'</div>')