var aa = 0;
var arr = new Array("one", "two", "three");
var arr1 = new Array("four", "five", "six");
function cl(me) {
me.clicked = !me.clicked;
var id = me.id;
if (me.clicked) {
if (id == 1000) aa = arr;
if (id == "one" || id == "two" || id == "three") aa = arr1;
var s = document.getElementById("sample");
var d = document.createElement("div");
d.id = me.id + 1;
s.appendChild(d);
for (i = 0; i < aa.length; i++) {
var l = document.createElement("label");
l.innerHTML = aa[i];
l.id = aa[i];
l.onclick = function (){cl(this)};
d.appendChild(l);
}
} else {
var child = document.getElementById(me.id + 1);
var parent = document.getElementById("sample");
parent.removeChild(child);
}
}
Markup:
<label id="1000" onclick="cl(this)">click</label><br><div id="sample"></div>
When the label "click" is clicked, three labels "one" "two" "three" are generated successfully.
When I click label "one" labels "four" "five" "six" are generated successfully.
When I again click label "one" "three four five" labels are removed.
But the problem is when I click label "click" and then label "one" .. now when i click "one" again the label "one two three" is alone removed but not "four five six".
document.getElementById- sergzachids of DOM elements to store keys to vital parts of the code. - Marcel Korpel