Have an idea?

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

Use total amount in BYO (ACBC) as part of a summation on same page.

In an ACBC study:

I want to use the total price of chosen attribute levels in a summation on the same page - as part of the 'FormatNumber' function, among other numbers.

Is this possible?
And if so; how?

Thanks in advance!

Best,
Matthijs
asked Sep 16 by anonymous
Sawtooth Script like FormatNumber is ran on the server, so it cannot be used dynamically on a survey page.  Instead, we'll need to recreate the behavior you are after using JavaScript.  Can you describe to me how you want the formatted number to look?

Getting the code to run each time a response is changed shouldn't be too bad.  Try this script:

<script>
$(document).ready(updateSummedPriceText);
$('#[% QuestionName() %]_div select').change(updateSummedPriceText);
SSI_CustomGraphicalRadiobox = updateSummedPriceText;

function updateSummedPriceText() {
    var interval = setInterval(function(){
        clearInterval(interval);
        var price = Number($('#[% QuestionName() %]_div .total_price .acbc_byo_price_box').val().replace(/[^0-9.]/g, ''));
        alert(price);
    }, 100);
}
</script>


If your summed price potentially includes decimals and your decimal character is not the period, line 9 would need to be updated.

Line 10 currently just shows an alert with the new price; this is what would be changed depending on what you want done with the current summed price.
Hi Zachary,

Thanks a lot for your reply!
I would like the number to be in a box below the BYO exercise.
Something like:
The total amount you will pay is : [BOX]

By the way; the number that should be in the box is the sum of numbers that are a result from previous questions (through 'SetValue') en the total amount corresponding to chosen levels in the BYO exercice.

Best,
Matthijs
I'm not sure I totally understand.  The "[BOX]" amount isn't just the value pulled from the summed price box of the BYO?  Or it is and its just the level prices that fluctuate depending on previous variables?
Indeed, the amount in the "[BOX]" is not just the value pulled from the summed price box.

The amount in the "[BOX]" is (value in summed price box) + (several values that are based on answers in previous questions*)

* = These values are not listlabels but determined with the [% GetValue %] function.

I hope this makes it clear. If not, please let me know!

Best, Matthijs
I'd start by adding something like this to the ACBC, wherever you want it to appear:

The total amount you will pay is: <span class="mySummedPrice"></span>


In my script above, between lines 9 and 10, we can add the prices from previous variables.  For something like a numeric question, it would just look like this:

price += [% NumericQ %];


The code might need to be a little different if you are using a different question type to store the value to add.

Then we can replace this example alert:

alert(price);


with code that will change the HTML we added at the very beginning:

$('.mySummedPrice').text(price);


That should get the basic functionality going.  We still haven't added in the number formatting, but I'd need to know exactly how you want the number formatted first.  Commas as thousands?  A certain fixed number of decimal points?

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.
...