Forum Moderators: open

Message Too Old, No Replies

how to target an exising div with javascript?

how to use JS to document.write to an existing div

         

digsafe

7:52 pm on Sep 13, 2010 (gmt 0)

10+ Year Member



Hi,
I am trying to figure out if there is a way to use javascript to write into an existing div. I am building a set of divs based off of a length of elements in an xml document.

So it would be kind of like this;

<body>
<div class="shell" id="shell_div"></div>
<script type="text/javascript">
//
var myXMLnumber = myXMLelements.length;
//
for(i=0;i<myXMLnumber;i++){
//
//---ok here is where I need some help---
//---how can I do a document write to an existing div?---
//---I would have thought it should look like this;---
//
document.getElementById("sales_main").write('<div>');
}
</script>
</body>

Any advice appreciated!

Fotiman

8:35 pm on Sep 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld! Assuming you're not using any sort of framework (jQuery, YUI, etc.), you'll want to append to the DOM. Something like this:


var sales_main = document.getElementById('sales_main');
var div = document.createElement('div');
div.innerHTML = "<h1>Hello World</h1>";
sales_main.appendChild(div);