3

I want to make dynamic division on button click on my web page please tell me easiest solution, I am new to JavaScript.

2

4 Answers 4

3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
is it ok now Felix Kling ;)
2

To create an element, use the createElement method

var mydiv = document.createElement('div');
//mydiv is a variable containing a newly created div

2 Comments

and document.body.appendChild(mydiv)
@GrzegorzKaczan the OP only mentioned creating one
0
<button onclick="createDiv()">Click Me</button>

function createDiv(){
   var newDiv = document.createElement('div');
}

Comments

0

//create a div like below

var div=document.createElement("div");
var node=document.createTextNode("This is new.");
div.appendChild(node);

Then append the above div to which you want it to be child

var element=document.getElementById("some_parent_tag");
element.appendChild(div);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.