Question T441
Visible to All Users

Access custom property from valuechanged

created 7 years ago

i would like to add a custom property 'points' to a survey , but i when i try to get the value i get undefined returned, am i missing something. i just want to add all the points earned in a survey for a score at the end.
adding the custom property with a default of 0
Survey.JsonObject.metaData.addProperty("itemvalue", { name: "points", default: "0" });

survey.onValueChanged.add(function (sender, question) {
var thepoints = question.points;
});

Show previous comments (4)

    Here is the code:

    JavaScript
    var points = 0; var val = options.question.value; var choices = options.questions.choices; for(var i = 0; i < choices.length; i ++) { if(choices[i].value == val) { points = choices[i].points; break; } }

    Why can't you set your points to the value directly?

    Thank you,
    Andrew
    SurveyJS Team

      Phil,

      You can count total score in the onComplete event, here is working sample:
      https://plnkr.co/edit/zKLFjKHGeHeYu08mO9sV?p=preview

      The code:

      JavaScript
      survey .onComplete .add(function (result) { var totalScore = result.getAllQuestions().reduce(function(total, q) { if(Array.isArray(q.choices)) { return q.choices.reduce(function(s, c) { if(q.value.indexOf(c.value) !== -1) { s += c.points } return s; }, total); } return total; }, 0); console.log(totalScore); });

      Thanks, Serge
      SurveyJS Team

        Superb Serge, works perfect thank you so much to you and andrew for your help on this.