How to display JSON data containing arrays using Jquery
How to display JSON data containing arrays using Jquery
HTML
<div id="radioButtonList"></div>
JavaScript
$(document).ready(function () {
var data = {
"17": { "answer_id": "17", "answer": "You earn a salary", "is_correct": "N" },
"18": { "answer_id": "18", "answer": "You earn profit", "is_correct": "N" },
"19": { "answer_id": "19", "answer": "You invest money", "is_correct": "N" },
"20": { "answer_id": "20", "answer": "All of the above.", "is_correct": "Y" }
};
$.each(data, function (i, entity) {
$('#radioButtonList').append($('<input />', { 'type': 'radio', 'name': 'answerRadioButtonList', 'id': entity.answer_id, 'value': entity.is_correct })).append(entity.answer + '<br />');
});
$('#radioButtonList').find(':radio').live('click', function () {
if ($(this).val() === 'Y') {
alert('Correct Answer.');
}
});
});