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

All 3 Replies

Google for createElement and insertBefore.

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

<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')
window.onload = createDiv;

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 {
Be a part of the DaniWeb community

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