Have an idea?

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

javascript help with "total" variable

I have a Free Format type question that has 3 possible items with a numeric entry.  At the bottome I'm showing a "total" of all the answers entered for the respondent to see and to be used later in another question to show the total from this question of all responses.  I had it working using the following javascript in the head tag (and using an onblur event handler on each item):

function calcSum()
{
var Sum = 0;
Sum = Number(document.getElementById("QS7_r1").value)
+ Number(document.getElementById("QS7_r2").value)
+ Number(document.getElementById("QS7_r3").value);
document.getElementById("QS7_tot").value = Sum;
}

The problem I'm running into is each item (QS7_r1, _r2 and _r3) is brought in using a constructed list in the previouse multiple response question (QS6).  If all 3 items are selected at QS6, the above works fine.  If one is not selected then it ceases to work.  I'm assuming the problem comes into play because it's attempting to add variable in the sum that didn't occur.  I've tried using variables set to 0 and then using the value only if that item occured in the previous question (see below).  Here is what I tried, but it's not working.  Any ideas?

function calcSum()
{
var var1 = 0;
var var2 = 0;
var var3 = 0;
if (document.getElementById("QS6_1").value == 1) {var1 = Number(document.getElementById("QS7_r1").value);}
if (document.getElementById("QS6_2").value == 1) {var2 = Number(document.getElementById("QS7_r2").value);}
if (document.getElementById("QS6_3").value == 1) {var3 = Number(document.getElementById("QS7_r3").value);}
var Sum = 0;
Sum = var1 + var2 + var3;
document.getElementById("QS7_tot").value = Sum;
}
asked Jan 10, 2012 by Jay Rutherford Gold (26,135 points)

1 Answer

0 votes
Jay, I think you're right, that you need to account for whether the id is there or not.  Using your code, here's what I would do (hope this is thorough enough)"


The code below has been changed.  Basically if the row is on the page, then we add it to their respective variable.  Finally we sum the variables of elements on the page.  Let me know if I'm still missing the mark.
function calcSum() { 
  var var1 = 0;
  var var2 = 0;
  var var3 = 0;
  var Sum = 0; 

  if ( document.getElementById("QS7_r1")  ) {
    var1 = Number(document.getElementById("QS7_r1").value);
  }

  if ( document.getElementById("QS7_r2")  ) {
    var2 = Number(document.getElementById("QS7_r2").value);
  }

  if ( document.getElementById("QS7_r3")  ) {
    var3 = Number(document.getElementById("QS7_r3").value);
  }

  Sum = var1 + var2 + var3; 
  document.getElementById("QS7_tot").value = Sum;
}
answered Jan 10, 2012 by Ryan Bronze (695 points)
edited Jan 10, 2012 by Ryan
unfortunately this still doesn't work.  Now, the only thing different I saw in your code was you added "document.getElementById("QS6_1") && " to the logic and I wasn't sure what that was for.  All 3 items will always come up at QS6, so each should always have a value of 1 if selected and 0 if not.  Then I build a list for the items to come up in QS7.  I don't know if it matter, but here is one of the items in that list construction:

<tr>
  <td> Cardiopulmonary Bypass (CPB) Procedures </td>
  <td> <center><input name="QS7_r1" id="QS7_r1" type="text" size="5" onblur="calcSum()"></center> </td>
</tr>

Then at QS7 I show the list label [%ListLabel(QS7RowList,1)%], if that item was never selected in QS6 then obviously none of that syntax from that list member comes up.
qs7 is on a different page than qs6?
Yes, two different pages.
okay, I adapted the code.  let me know what you think.
OF COURSE!!!!! As I smack my head against the desk....  Works perfectly.  Thanks a ton for the assistance on this Ryan, sometimes the simple things just elude us.  Well I can end my night on a good note, thanks again, mucho appreciated.
No problem!  Happens to all of us. :)  Glad we were able to arrive at a solution.
...