Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Need script to do Constant Sum TOTAL in Free Form Numeric Entry List

We have a very specific format for a question that otherwise would just be a constant sum.  We are able to implement the format (including things like recalling prior answers to remind user, etc) using the Free Form design via HTML.

The issue that we are having is that we cannot verify that the numeric lists (there are two) each total to 100 and therefore cannot be sure that the data we collect will be usable.

If anyone is able to help, we should be able to share screen and go right to the question.  Alternatively, if you have some old javascript or perl (not sure which is appropriate), that you can annotate a little, we can do our best to repurpose it.

Many thanks for any help you can provide.

-Dan Ross
asked Jun 5, 2012 by spiegeld (120 points)
retagged Sep 13, 2012 by Walter Williams

2 Answers

+1 vote
I have a setup with exactly what you mentioned.  Free format performing just like a constant sum question.  I have a free format question called 'Q7' with 5 rows and a total row defined (Q7_r1 through Q7_r5 and Q7_tot.

Under the advanced settings option on the HTML <Head> Tag I have:

<script type="text/javascript">

function calcSum()
{
var Sum = 0;
Sum = Number(document.getElementById("Q7_r1").value)
+ Number(document.getElementById("Q7_r2").value)
+ Number(document.getElementById("Q7_r3").value)
+ Number(document.getElementById("Q7_r4").value)
+ Number(document.getElementById("Q7_r5").value);
document.getElementById("Q7_tot").value = Sum;
}
</script>

Then on the Custom Javascript verification tab I have:

var var1=SSI_GetValue ("Q7_r1")
var var2=SSI_GetValue ("Q7_r2")
var var3=SSI_GetValue ("Q7_r3")
var var4=SSI_GetValue ("Q7_r4")
var var5=SSI_GetValue ("Q7_r5")
var vartot=var1+var2+var3+var4+var5;
if (vartot != 100) {strErrorMessage = "Your total must equal 100%";}

And lastly, here is the grid setup for reference:

<table border="1" cellpadding="1" cellspacing="0" width="60%">
<tr>
  <td width="40%"> <b>Animal Type</b> </td>
  <td> <b>% Personal Contact Time</b> </td>
</tr>
<tr>
  <td> Canine  </td>
  <td> <input name="Q7_r1" id="Q7_r1" type="text" size="3" onblur="calcSum()">% </td>
</tr>
<tr>
  <td> Feline </td>
  <td> <input name="Q7_r2" id="Q7_r2" type="text" size="3" onblur="calcSum()">% </td>
</tr>
<tr>
  <td> Avian (other than poultry) </td>
  <td> <input name="Q7_r3" id="Q7_r3" type="text" size="3" onblur="calcSum()">% </td>
</tr>
<tr>
  <td> Exotic pets </td>
  <td> <input name="Q7_r4" id="Q7_r4" type="text" size="3" onblur="calcSum()">% </td>
</tr>
<tr>
  <td> Other Pets or Animals </td>
  <td> <input name="Q7_r5" id="Q7_r5" type="text" size="3" onblur="calcSum()">% </td>
</tr>
<tr>
  <td> <b>TOTAL</b> </td>
  <td> <b><input name="Q7_tot" id="Q7_tot" size="3">%</b> </td>
</tr>
</table>

This will update the total as the answers are put in and will require it to add to 100%.  Hope this helps you.
answered Jun 5, 2012 by Jay Rutherford Platinum (53,170 points)
Ridiculously on-target.  Saved me a whole bottle of excedrine.  :-)
Glad I could help!
the page is not going forward
0 votes
I remember a function on help. Autosum or such, give it a try.
answered Jun 5, 2012 by Bahadir Ozkurt Gold (17,080 points)
The total is 5 than error show
...