I want to make dynamic division on button click on my web page please tell me easiest solution, I am new to JavaScript.
-
Have a look at w3.org/wiki/Creating_and_modifying_HTML, elated.com/articles/javascript-dom-intro and developer.mozilla.org/en-US/learn/javascript in general.Felix Kling– Felix Kling2013-04-16 10:25:11 +00:00Commented Apr 16, 2013 at 10:25
-
1@Android Programmer - Welcome to SO. Please do look around once to check if a similar question has been asked and answered elsewhere. Happy coding :)Alok Swain– Alok Swain2013-04-16 10:25:52 +00:00Commented Apr 16, 2013 at 10:25
Add a comment
|
4 Answers
Hope this will help
function divcreate() {
var div = document.createElement("div");
div.setAttribute("id", "mydiv");
div.className = "mdiv";
div.style.display = "none";
document.body.appendChild(div);
}
2 Comments
Felix Kling
Why do you use
div.setAttribute('id',...) and not div.id = ...;? And why do you have so much space on the left side of your code?MR. Kumar
is it ok now Felix Kling ;)
To create an element, use the createElement method
var mydiv = document.createElement('div');
//mydiv is a variable containing a newly created div
2 Comments
Grzegorz Kaczan
and document.body.appendChild(mydiv)
Joseph
@GrzegorzKaczan the OP only mentioned creating one