thuk
thuk

Reputation: 271

how to add element within an element in html

i need to add a element div and within that div i need to add a label element how to do that.....??

var dd=document.getElementById("sample");
var d=document.createElement("div");
d.id="s";
d.innerHTML="welcome"
dd.appendChild(d);
var e=document.createElement("label");
e.innerHTML="success";
var f=dd.getElementById("div");
f.appendChild(e);

i have a div element sample in html..<div id="sample"></div> within that div element i add another div element with id "s" then i need to a label within the div id="s" how to do that????????

Upvotes: 0

Views: 400

Answers (4)

JeffSahol
JeffSahol

Reputation: 971

dd.appendChild(d).appendChild(s)

Upvotes: 0

wong2
wong2

Reputation: 35710

var dd = document.getElementById("sample");
var d = document.createElement("div");
d.id = "s";
d.innerHTML = "welcome"
var e = document.createElement("label");
e.innerHTML = "success";
d.appendChild(e);
dd.appendChild(d);

Upvotes: 0

Dustin Laine
Dustin Laine

Reputation: 38503

You were close.

var dd = document.getElementById("sample");

var d=document.createElement("div");
d.id = "s";
d.innerHTML="welcome"

var e = document.createElement("label");
e.innerHTML="success";

dd.appendChild(d);
d.appendChild(e);

http://jsfiddle.net/FzhYz/

Upvotes: 2

Quentin
Quentin

Reputation: 943142

d.appendChild(e);

Note that HTMLElementNodes do not have a getElementById method.

Upvotes: 0

Related Questions