Forum Moderators: open

Message Too Old, No Replies

add element to <div>

         

jelleast

10:58 pm on Nov 15, 2006 (gmt 0)

10+ Year Member



here i am again...

is there a way to add an element like <div> to an existing <div>?

say i have:
<div id="container">
<div id="1"></div>
<div id="2"></div>
</div>

and i want to add a third div...

what kind of code would i have to use?

thanx alot :)
greetings jelle

birdbrain

5:15 pm on Nov 16, 2006 (gmt 0)



Hi there jelleast,

does this help...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
<!--
window.onload=function(){
dv=document.createElement('div');
txt=document.createTextNode('this is d3');
dv.appendChild(txt);
document.getElementById('container').appendChild(dv);
}
//-->
</script>

</head>
<body>

<div id="container">
<div id="d1">this is d1</div>
<div id="d2">this is d2</div>
</div>

</body>
</html>

birdbrain

Fotiman

5:22 pm on Nov 16, 2006 (gmt 0)

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




<div id="container">
<div id="1">1</div>
<div id="2">2</div>
</div>
<script type="text/javascript">
function addToContainer()
{
// Get the parent node
var c = document.getElementById("container");
if(!c ) return;
// Create a new div
var d = document.createElement("div");
// Create a text node to go in the div
var t = document.createTextNode("Hello World!");
// Stuff the text node into the new div
d.appendChild(t);
// Add the new div to container
c.appendChild(d);
}
window.onload = addToContainer;
</script>

See the DOM spec for more info.
[w3.org...]

jelleast

10:59 pm on Nov 16, 2006 (gmt 0)

10+ Year Member



YEAH!

thanx alot :D