I'm using jquery to make a simple quiz. I'm trying to get the question, answer (content) and answer (index) into an object. Here is my code so far and a fiddle.
$('.submit').on('click', function() {
$('li[data-question]').each(function(i){
question = $(this).attr('data-question');
answer = $(this).find('input:checked').parent().text().trim();
index = $(this).find('input:checked').parent().parent().parent().index() + 1;
$('body').data(question, {
"question":question,
"index":index,
"answer":answer
});
console.log( $('body').data() );
});
});
http://jsfiddle.net/oe6z9svq/2/
That works, but I want to just get the final object of responses out of it. Currently it steps through each question and makes an object for each one, adding one at a time.
How can I return(?) the final result as json then use it elsewhere?