The previous example used a user-defined function in the code mask field. This is a convenient way of doing it, but there are a few pitfalls with this approach.
The code mask is called several times when you go through the questionnaire, not only when the question is displayed. Every time you use the f function on a question with a code mask, the code mask is evaluated. So the function in the code mask of a question will be called every time there is a reference to the question in scripts elsewhere in the survey.
Due to this, the time it takes for the respondent to load the survey pages will increase slightly because there are more scripts to execute.
You are therefore recommended to be cautious when using functions in code masks. An alternative approach is to set a hidden multi question and filter on that instead. This hidden multi question can also be useful for reporting purposes.
Using a Hidden Multi to Filter an Answer List Based on a Grid
As described previously (go to add and remove for more information), we have a grid where the elements are given a 1-5 rating, and we want the next question to use only the elements that are given a score of "4" or "5". Now let us use a script to set a hidden multi question, scorefilter, instead of using a function. The grid has question ID carrating.
var form = f("carrating");
var codes = form.domainValues();
var i : int;
for(i=0;i<codes.length;i++)
{
var code = codes[i];
if(form[code].get() == "4" || form[code].get() == "5")
{
f("scorefilter")[code].set("1");
}
else
{
f("scorefilter")[code].set("0");
}
}
var form = f("carrating");
var codes = form.domainValues();
for(var i = 0; i < codes.length; i++) {
var code = codes[i];
if (form.item(code).get() == "4" || form.item(code).get() == "5") {
f("scorefilter").item(code).set(1);
}
else {
f("scorefilter").item(code).set(0);
}
}
Then the code mask uses scorefilter instead of the function call:
f("scorefilter")