my name is xyz
my name is xyz

Reputation: 349

Javascript Appendchild

This is my code

for(var i=0;i<Year12.length;i++)
      {

                var div1=document.createElement('li');
                 div1.setAttribute('id',Year12[i]);
                 div1.setAttribute('Value',Year12[i]);
                 document.getElementById("Jan").appendChild(div1);
                 alert(div1);


       }

"jan" is the id of an UL element.I am able to create the Element. But Its not adding to the parent element.Can any one try this

Upvotes: 0

Views: 1141

Answers (2)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

Remove Value and try:

var div1=document.createElement('li');
div1.setAttribute('id',Year12[i]);
div1.innerHTML= Year12[i];
document.getElementById("Jan").appendChild(div1);
alert(div1);

OR

var div1=document.createElement('li');
    div1.setAttribute('id',Year12[i]);
    div1.appendChild(document.createTextNode(Year12[i]));
    document.getElementById("Jan").appendChild(div1);
    alert(div1);

For setting class, you can do:

div1.className = "your_class_name";

Upvotes: 2

Jitendra Pancholi
Jitendra Pancholi

Reputation: 7562

try this

for(var i=0;i<Year12.length;i++)
{

    var div1=document.createElement('li');
    div1.setAttribute('id',Year12[i]);
    div1.setAttribute('Value',Year12[i]);
    //document.getElementById("Jan").appendChild(div1);
    document.getElementById("Jan").append(div1);
    alert(div1);
}

Upvotes: 0

Related Questions