Dchris
Dchris

Reputation: 3067

can't create html content in a loop

I am trying to create new article html elements under a section html element in a loop using javascript that reads content from xml but what i get is only the last article of the loop.

//variable holds a specific section element
var section = document.getElementById('articles');

//monument variable holds 2 monument elements from xml
for (var i = 0; i < monument.length; i++) {

    //create articles elements
    var article = document.createElement('article');

    //create the title of the article
    article.innerHTML += "<h3>";

    //the title variable holds 2 title elements from xml
    article.innerHTML += title[i].childNodes[0].nodeValue;
    article.innerHTML += "</h3>"

    //append article to section
    section.appendChild(article);

} //end of for

Can you figure out why?

Upvotes: 0

Views: 64

Answers (1)

Chen-Tsu Lin
Chen-Tsu Lin

Reputation: 23244

I assign two elements arrays to your variables for test.

var monument = ['1', '2'],
    title = ['1', '2'];

http://jsfiddle.net/YP7fj/

It works fine.

So, The problem is in your monument or title variables.

There is no any problem in the code you showed.

Upvotes: 1

Related Questions