Skip to main content

Kinesis Support

All the topics, resources needed for Kinesis.

FocusVision Knowledge Base

Numeric Examples

This page contains examples and demos for numeric question types and commonly used math-based computationals.

For more general information on this question type, see Question Types. For commonly used data validation, see the Response Validation article.

1:  Computational Counter Based on Conditional Statements

This example will increment by 1 for each true condition within the computational and return the counter value when completed.

# Counter begins at 0 and increases by 1 for each true condition. 
$USER_counter = 0;

if ($Q1a==1)  $USER_counter ++;
if ($Q2 > 5)  $USER_counter ++;
if ($Q3[1]==1 OR $Q3[2]==1)  $USER_counter ++;

return $USER_counter;

2:  Sumup / Constant Sum Matrix Tables

Sumup (constant sum) matrix tables are simple to create, but require some javascript to customize. The below scripts can be embedded into the HTML of the page to manipulate how the table looks and behaves.

2.1:  Change Width of Total/Remaining Fields

This script is ID based - specify the ID (#total, #remaining) you'd like to modify. Change the value "X" to your desired pixel length:

<script type="text/javascript"><!--
$(document).ready(function() {
	$("#total_1").css("width", X);
});
// --></script>

2.2:  Add Prefix or Postfix to Summation Fields

Prefix: This example adds "$" before the total field:

<script type="text/javascript"><!--
$(document).ready(function() {

	$("#total_1").before("$");
});
// --></script>

Postfix: This example adds "%" after the total field:

<script type="text/javascript"><!--
$(document).ready(function() {

	$("#total_1").after("%");
});
// --></script>

2.3:  Combining Modifications into Single Script

The above javascript customizations can also be combined into a single script call. E.g.:

<script type="text/javascript"><!--
$(document).ready(function() {

        $("#total_1").css("width", 55);
	$("#total_1").after("%");

        $("#remaining_1").css("width", 55);
	$("#remaining_1").after("%");
});
// --></script>

2.4:  Customize Remaining Field Value

Currently when adding Total and Remaining cells to a number table the remaining value is calculated using the default total value of 100. This is fine for percentages but unnecessary if you are using a number other than 100. To change this default value follow the steps below.

VIEW DEMO

  • Create the number table formatted as necessary.
  • From the table editor right-click the column with the number field. In the dialogue box select "Total + Remaining" from the Sumup pulldown. This will add your Total and Remaining fields onto the end of your table.
  • Create an extra text question after the table but on the same page.
  • Insert the following code into the Extra Text question's HTML - you MUST use "NO EDITOR" mode, or this code will display as plain text:
<script type="text/javascript">
function dosumup(mode,id,labels)
{
    var total=0;
    for (i=0;i<labels.length;i++)
    {
        if (document.getElementById(labels[i]))
        {
            var value=document.getElementById(labels[i]).value;
           
            if (parseFloat(value)!="NaN")
            {
                total+=Math.round(value);
            }
        }
    }
    if (mode=="both" || mode=="total")
    {
        document.getElementById("total_"+id).value=total;
    }
    if (mode=="both" || mode=="remaining")
    {
        document.getElementById("remaining_"+id).value=(CUSTOMTOTAL-total);
    }
}
</script>
  • Change the value "CUSTOMTOTAL" on the fourth line from the bottom to your custom number to base the remaining total off of. This can be piped in from a previous question using the standard piping method ~Qlabel~, as long as the question being piped from is returning a number.
  • Run the Survey Tester and check that everything works correctly.

3:  Update Number Fields Automatically

Use the following functions in an Extra Text question on the same page of the related number questions. In the example code provided - within the $(document).ready() method - the question labelled Q3 will contain the sum of Q1 + Q2 while the question labelled Q5 will contain the difference of Q3 - Q4.

 <script type="text/javascript">// <![CDATA[
 //
 // defines the addition operation
 function addQ(Qa, Qb) {
   return parseInt($("#"+Qa).val()) + parseInt($("#"+Qb).val());
 }
 // defines the subtraction operation
 function subtractQ(Qa, Qb) {
   return parseInt($("#"+Qa).val()) - parseInt($("#"+Qb).val());
 }
 
 $(document).ready(function() {
   //
   // optionally prevents manual editing of the target fields
   //$("#Q3").attr('readonly', 'readonly');
   //$("#Q5").attr('readonly', 'readonly');

   // triggers the calculation for Q3 when either Q1 or Q2 is changed
   $("#Q1").change(function() {
     $("#Q3").val(addQ("Q1", "Q2"));
   });
   $("#Q2").change(function() {
     $("#Q3").val(addQ("Q1", "Q2"));
   });

   // triggers the calculation for Q5 when either Q3 or Q4 is changed
   $("#Q3").change(function() {
     $("#Q5").val(subtractQ("Q3", "Q4"));
   });
   $("#Q4").change(function() {
     $("#Q5").val(subtractQ("Q3", "Q4"));
   });
 });
 //
 // ]]></script>
  • Was this article helpful?