i have tried a few tutorials but i cant get this working. I am trying to create a div with the id of overlay and insert it right after the body tag when a function is run. can anyone help? thanks

Recommended Answers

Google for createElement and insertBefore.

Jump to Post

All 3 Replies

Google for createElement and insertBefore.

Here's a simple demo, to get you started!

<html>
<head>
<title><!--Sample--></title>
<script language="JavaScript"type="text/javascript">
<!--
function createDiv() 
{ var _body = document.getElementsByTagName('body') [0];
var _div = document.createElement('div');
var _text = document.createTextNode('Creating Div Element')
_div.appendChild(_text);
_body.appendChild(_div);
}
window.onload = createDiv;
-->
</script>
</head>
<body>
</body>
</html>

If you need something else just let me know. Good day to you...

thanks for the replies i ended up doing this

function createDivs() {
var div = document.createElement('div');
		var container = document.createElement('div');
    	div.id = 'overlay';
		container.id = 'form_container';
if (document.body.firstChild){
      	document.body.insertBefore(div, document.body.firstChild);
		document.body.insertBefore(container, document.body.firstChild);
		} else {
      	document.body.appendChild(div);
		document.body.appendChild(container);
		}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.