Register  |  Login

Question Details    

   Question

Time: 20:47 - Sep 11, 2006     Asked by: david      Status: Answered      Points: 50   

How do I create a DIV with javascript?

I want to create a DIV manually with javascript. How is that accomplished?

Ask a New Question

Become a Quomon Expert

Current Categories

 

Similar Questions


   

Answer Discussion
Answer Discussion
Answer Summaries
Answer Summary
 
I found out myself:

var newdiv = document.createElement('div');
var divIdName = 'testDiv';
newdiv.setAttribute('id',divIdName);
newdiv.style.width = "300px";
newdiv.style.height = "300px";
newdiv.style.left = "300px";
newdiv.style.top = "300px";
newdiv.style.position = "absolute";
newdiv.style.background = "#00C";
newdiv.style.border = "4px solid #000";
newdiv.innerHTML = 'Test Div';
document.body.appendChild(newdiv);

Expert:

david

Date:

Sep 12, 2006

Time:

09:16

 

Votes: Good (0) | Bad (0)
Login to rate this answer

good work...now give me your points...lol

Expert:

multani.sarbjit

Date:

Sep 12, 2006

Time:

11:58

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Ok, good enough david, but what I usually do is to define the DIV in the html code like this (example with some of your attributes):

<div name="testDiv" id="divID" style="width: 300px; height: 300px; left: 300px; top: 300px; position: absolute; display: none">
Test Div
</div>

Note the "display: none" which hides it initially.

When you want to show it, just execute

document.getElementById("divID").style.display = "";

You can also create copies of it in javascript and - of course - hide it again.

I think it makes it simpler to keep layout in the html file and only let javascript handle execution.

Jakob

Expert:

jgivoni

Date:

Sep 13, 2006

Time:

15:17

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Thanks, Jakob, but I needed the complete dynamic approach since it will be used on different html-pages without me being able to alter the html on beforehand.

Expert:

david

Date:

Sep 20, 2006

Time:

11:25

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Question Answered

This question has been answered, and points have been rewarded to the following experts:

david: 40
jgivoni: 10

You're welcome however to comment or give additional information or if you wish, you have the ability to write an Answer Summary for this question by clicking on the "Answer Summaries" Tab.

 
No summaries have been submitted yet. Want to be the first?



Respond to this question:

New User

  Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:


Forgotten Password

 

New User

  Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:


Forgotten Password

   

"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it


All Questions


Language Options

English:

www.quomon.com

Español:

www.quomon.es

Sponsors

Questions and Answers Software
Real Estate Postcards
Marketing Fulfillment