lascoff
lascoff

Reputation: 1331

JavaScript object not working in IE

The code below works on all browsers I have tried but does not work on IE. The jsonObj at ends up with all nulls. (Browsers tested Mac safari, chrome, firefox, PC FireFox, Opera, chrome) IE is the only one to fail. Can some one see my problem?

IE version 10

function Save() {
    var path = document.location.pathname;

    var Checked = "{";
    jsonObj = [];

    $('.questionsOnPage').each(function () {

        item = {}
        var id = this.id;

        jQuery(this).children(".questionCheckBox").each(function () {
            item ["id"] = this.id;
            item ["selected"] = this.checked;
        });

        jQuery(this).children(".question").each(function () {
            item ["question"] = this.innerHTML;
        });

        answers = {}

        jQuery(this).children(".answer").each(function () {
            answer = {};
            answer ["selector"] = $(this).attr("data-selector");
            answer ["answerText"] = $(this).attr("data-answerText");
            answer ["correct"] = $(this).attr("data-correct");
            answers [$(this).attr("data-selector")] = answer;
        });

        item["answers"] =  answers;

        jsonObj.push(item);
    });

Upvotes: 0

Views: 1701

Answers (1)

TwilightSun
TwilightSun

Reputation: 2335

As Deryck suggested I'll put this in the answer.

The solution to your problem: add Var before item={}

Different browser vendors have different implementation on the JavaScript engine. To my observation, when you push something into an array in IE, you're pushing a reference of the object instead of a cloned copy (not sure if this is true). So modifying item after pushing, would result in changing the previous pushed object. Adding var will assure that you'll get a new copy in each iteration step.

Upvotes: 5

Related Questions