I’m trying to create an object with key value pairs in a loop through some DOM elements.
Some background on the overall context of what I’m doing: this section of code is part of a questionnaire I’m creating.
This is the code in question:
var resultsView = {};
function sumAnswers(className) {
//some stuff here
$(className:checked).each(function() {
//some other code here to sum answers
var text = $(this).siblings('.question').text();
var answer = $(this).next('label').text();
resultsView[text] = answer;
});
}
I’m trying to add the question text and selected answer text in a key/value relationship to an object, that way I can later pass it through AJAX to a PHP file.
The problem is that when I console.log resultsView, it only adds one question/answer multiple times. Changing the operator to += isn’t working either. What am I doing wrong?
I am unable to see such a issue could you please check it in the below jsfiddle
http://jsfiddle.net/sZYaf/19/
I have create similar code in the above specified case
Thanks