The form objects can have the following properties, depending on the type of the question (x represents a form object returned from the f function):
x.CODED
CODED is true if the question referenced is a coded variable. If not, CODED is undefined. Coded variables are single questions and loops, and answer list items of a grid or a multi question.
x.OPEN
OPEN is true if the question referenced is an open variable. If not, OPEN is undefined. Open variables are open text questions, other-specify elements of answer lists and answer list elements of an open text list, numeric list or ranking question.
x.DICHOTOMY
DICHOTOMY is true if the question referenced is a dichotomy. If not, DICHOTOMY is undefined. The elements of a multi question (referenced with f(qID)[code]) are dichotomies.
x.COMPOUND
COMPOUND is true if the question referenced is a compound. If not, COMPOUND is undefined. Multi, open text list, numeric list, ranking and grid questions are compounds.
x.NUMERIC
NUMERIC is true if the question referenced is numeric. If not, NUMERIC is undefined.
x.EXTERNAL
EXTERNAL is true if the question referenced gets its answerlist from Database Designer (either as a lookup table or a hierarchy).
x.DATE
DATE is true if the question referenced is a date question. If not, DATE is undefined.
x.BOOL
BOOL is true if the question referenced is Boolean (has the Boolean property set). If not, BOOL is undefined.
x.GEO
GEO is true if the question referenced is a Geolocation question. If not, GEO is undefined.
Note: The Boolean property and Date question type are only available when using the Optimized database format.
These properties are extremely powerful, because by using them it is possible to write generic scripts that will work no matter what the question type is.
Using a combination of COMPOUND with DICHOTOMY and OPEN enables the user to differentiate between a grid and a multi and a numeric list, open text list or ranking question since the elements of a multi are a DICHOTOMY, but DICHOTOMY would be undefined for a grid, numeric list, open text list or ranking, and elements of numeric list, open text list or ranking are OPEN, but OPEN is undefined for grid and multi.
JScript example:
var form = f("q1");
if(form.COMPOUND)
{
var cats = form.domainValues();
var code = cats[0];
if(form[code].DICHOTOMY)
{
//form is a multi
}
else if(form[code].OPEN)
{
//form is a numeric list, open text list or ranking question
}
else
{
//form is a grid
}
}JavaScript example:
var form = f("q1");
if(form.COMPOUND)
{
var cats = form.domainValues();
var code = cats[0];
if(form.item(code).DICHOTOMY)
{
//form is a multi
}
else if(form.item(code).OPEN)
{
//form is a numeric list, open text list or ranking question
}
else
{
//form is a grid
}
}Deleting the Content of any Question
This function will remove the answers on any question, no matter what kind of question it is. This could for example be used to automatically remove respondent information at the end of the survey if you ask the respondents if they want to be anonymous or not. An answer is removed by overwriting the current value with null. It will work on open text, single, date, numeric, multi, open text list, numeric list, ranking and grid questions, and you use it like this:
ClearForm(f("q1"));for a question with question ID q1.
If the question is a compound, the function will remove answers in all variables in the compound. (It will however not automatically remove a value in an "Other, specify" field on a question. To remove the answer on "Other, specify" you have to call the function referring to the "Other, specify" field, e.g. like this:
ClearForm(f("q1_98_other"));if the "Other " property is set on the item in the answer list with code "98".)
Here is the definition of the function:
JScript example:
function ClearForm(form)
{
if(form.COMPOUND) //form with multiple items
{
var fcodes = form.domainValues(); //all codes in form
for(var i : int = 0;i<fcodes.length;i++) //iterate through code
{
form[fcodes[i]].set(null); //clear item
}
}
else //form with one item
{
form.set(null);
}
}JavaScript example:
function ClearForm(form)
{
if(form.COMPOUND) //form with multiple items
{
var fcodes = form.domainValues(); //all codes in form
for(var i = 0;i<fcodes.length;i++) //iterate through code
{
form.item(fcodes[i]).set(null); //clear item
}
}
else //form with one item
{
form.set(null);
}
}Copying the Contents of any Form into Another
Here is a function that will copy any type of question. It will return true if the copying succeeds, and false without copying anything if it does not. (If the from and to questions were not the same type of questions).
JScript example:
function CopyForm(from,to) : Boolean
{
if((from.CODED && to.CODED) || (from.OPEN && to.OPEN) || (from.DICHOTOMY && to.DICHOTOMY)|| (from.DATE && to.DATE) || (from.EXTERNAL && to.EXTERNAL))
{
to.set(from.get());
return true;
}
else if(from.COMPOUND && to.COMPOUND)
{
var fcodes = from.domainValues();
var tcodes = to.domainValues();
if(fcodes.length == tcodes.length)
{
for(var i : int = 0;i<fcodes.length;i++)
{
if(fcodes[i].toString()==tcodes[i].toString())
{
to[fcodes[i]].set(from[fcodes[i]].get());
}
else
{
return flase;
}
}
return true;
}
}
return false;
}JavaScript example:
function CopyForm(from,to) : Boolean
{
if((from.CODED && to.CODED) || (from.OPEN && to.OPEN) || (from.DICHOTOMY && to.DICHOTOMY)|| (from.DATE && to.DATE) || (from.EXTERNAL && to.EXTERNAL))
{
to.set(from.get());
return true;
}
else if (from.COMPOUND && to.COMPOUND)
{
var fcodes = from.domainValues();
var tcodes = to.domainValues();
if(fcodes.length == tcodes.length)
{
for(var i = 0;i<fcodes.length;i++)
{
if(fcodes[i].toString()==tcodes[i].toString())
{
to.item(fcodes[i]).set(from.item(fcodes[i]).get());
}
else
{
return false;
}
}
return true;
}
}
return false;
}