0

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????????

Quentin
949k137 gold badges1.3k silver badges1.4k bronze badges
asked May 27, 2011 at 17:09
1

4 Answers 4

2

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/

answered May 27, 2011 at 17:14
Sign up to request clarification or add additional context in comments.

1 Comment

a problem jsfiddle.net/saravanabalaji/XHV7X/3 . look at this. when we click "gaming" then "os" a textbox will appear. when we again click os the textbox will disappear. but the problem is when we again click the browser the os and textbox must disappear but os alone disappear.. suggestion ...........
0
d.appendChild(e);

Note that HTMLElementNodes do not have a getElementById method.

answered May 27, 2011 at 17:12

Comments

0
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);
answered May 27, 2011 at 17:14

Comments

0
dd.appendChild(d).appendChild(s)
Tamara Wijsman
12.4k8 gold badges56 silver badges82 bronze badges
answered May 27, 2011 at 17:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.