Javascript error/calculation fail NaN/NaN/NaN | XM Community
Skip to main content

Hi,
We have a survey where users enter a start date, and we have javascript on this start date field that populates a hidden/embedded data field called endDate (yes we realized after the fact that we used the qualtrics core metadata field name, this is not causing issues with the code). We then pipe text the start and end date into subsequent questions/fields in the survey for the user to report on this specific date range. We have 6 responses so far, and 5/6 have failed to calculate the endDate and the endDate is appearing to the users and being saved in the data as "NaN/NaN/NaN". We realized that browser type/version may be playing a role in how the javascript executes, so have since included UserAgent as an embedded data field to capture this additional information for new records in the meantime, while we try to resolve the issue with endDate not populating correctly.
Anyone else in the community have any insight or additional suggestions on troubleshooting this issue further?
Thank you!
Rebecca

NaN stands for not a number, so you are most probably performing some calculations on variables that can not be coerced into numbers. Make sure that each step, you are performing calculations only on numbers.
It's difficult to say at which step this is happening without looking at your code.


Hi, thanks so much for the reply! It's just odd considering it worked for one record just fine, and this was the 4 record out of 6.
This is our code-
image.png


I'm not sure what the issue was. Maybe some internal things.
Try this:
Qualtrics.SurveyEngine.addOnReady(function () {
    let selector = "#" + this.questionId + " .InputText";
    jQuery(selector).flatpickr({
        dateFormat: "m/d/Y",
        onChange: function (selectedDates, dateStr, instance) {
            let dt = new Date(dateStr).fp_incr(29),
                year = dt.toDateString().split(" "),3],
                day = dt.toDateString().split(" ")i2],
                month = dt.getMonth() < 9 ? "0" + (dt.getMonth() + 1) : dt.getMonth() + 1,
                end_date = month, day, year].join("/");
            Qualtrics.SurveyEngine.setEmbeddedData("endDate", end_date);
            console.log(end_date);
        },
    });
});


Thanks! Will look into trying that. It's definitely odd, since it also worked fine for me in preview mode. Also exported the .qsf and imported as a new survey and worked just fine.


In that case I would recommend changing the name of the ED. Since it's one of the reserved ones.
Maybe that's creating the issue while running the survey, but not during the preview.


Thank you again! We have done some more testing and learned that the NaN error is thrown when taking the survey in IE, firefox, or safari, but it works fine/does not throw error in Chrome/Edge. Any additional thoughts/input there? Seems like it is related to javascript not playing nicely with certain browsers..


Nope. No problem here. See this link.

Flatpickr_Test.qsf


Leave a Reply