-1

I'm building a JSON array out of submitted data in a form, but this script is breaking and can't figure out what is causing it!

Any ideas?

Jquery:

    var canvas = [];

    var Submission = {};

    var Answer = {};

        $('.question-holder > input').each(function(){
        answer = $(this).val()
        input_name = $(this).attr('name')
        label = $(this).closest('li').find('label')

        // Push everything into the questions array
        Answer.push({
        Answers: answer,
        Input_Name: input_name,
        Label: label
        });         

        });

        $('.submit-holder > input').each(function(){
            answer = $(this).val()
            input_name = $(this).attr('name')
            label = $(this).closest('li').find('label')

            Submission.push({
            Question: label,
            Input_Name: input_name,
            Submitted_data: answer
            });                         
        });

        canvas.push({
        Submission: Submission,
        Answers: Answer,
        Motivation: $('.motivation').val()
        });

        json = JSON.stringify({json: canvas}, null, "\t");  
Kim
  • 1,128
  • 6
  • 21
  • 41
  • 1
    Can you be a bit more specific when you say "breaking". How? What error are you getting? – Adrian Wragg Aug 21 '13 at 23:30
  • I get this console error: "Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable." at the last line – Kim Aug 21 '13 at 23:45

1 Answers1

2

There problem is relating to the declaration:

var Submission = {};
var Answer = {};

They should be arrays like this, objects (hash) don't have push() defined

var Submission = [];
var Answer = [];
zs2020
  • 53,766
  • 29
  • 154
  • 219