I would like to add data from a JSON object to HTML markup. I use jQuery and AJAX to read out the data. What I want to achieve is that there will be add for each object in the JSON file a new HTML markup that represent an item.
I use the following code to add the text key from each object.
$.ajax({
url: 'api/videoData.js',
dataType: 'json',
success: function(data) {
console.log(data); //uncomment this for debug
for (var i = 0; i < data.content.length; i++) {
if (data.content[i].type == 'normal') {
console.log(data.content[i].text);
$('.article.text').find('p').html(data.content[i].text);
}
};
},
error: function() {
console.log('Houston, we have a problem!');
}
});
HTML Markup:
<div class="article text">
<span class="format text alignRight"></span>
<h4>Subtitle</h4>
<h3>Title</h3>
<p>
Paragraph text
</p>
</div><!-- End div.article text -->
JSON FILE:
{
"title": "London",
"category": "The Ultimate",
"image": "images/content/londen.jpg",
"website": "http://www.london.nl/",
"youtube": "https://www.youtube.com/watch?v=Sq9rjbouBlY&t=23",
"maps": "https://www.google.nl/maps/place/Londen,+Verenigd+Koninkrijk/@51.5114089,-0.1271477,13.79z/data=!4m2!3m1!1s0x47d8a00baf21de75:0x52963a5addd52a99",
"content": [
{
"type" : "normal",
"timeTrigger": 24,
"title": "London Eye",
"subtitle": "Algemene",
"picture" : "images/content/london.jpg",
"text": "This is the London Eye",
"readmore": "http://wikitravel.org/en/London"
},{
"type": "normal",
"timeTrigger": 24,
"title": "Sherlock Holmes",
"subtitle": "Detective",
"picture" : "images/content/sherlock_holmes.jpg",
"text": "Sherlock Holmes is een fictieve detective <br> uit de verhalen van de laat-19de-eeuwse",
"readmore": "http://nl.wikipedia.org/wiki/Sherlock_Holmes"
},{
"type": "normal",
"timeTrigger": 39,
"title": "Fish \"n Chips",
"subtitle": "Eten",
"picture" : "images/content/sherlock_holmes.jpg",
"text": "Fish and chips is een typisch Britse afhaalmaaltijd .",
"readmore": "http://youngandfoodish.com/london/top-10-fish-and-chips-in-london/"
}
]
}
It will console.log all the text of all my objects, but it doesn't appear in my HTML.
$('.article.text').find('p')selector keeps changing the content of the same element(s), what do you expect?response?