Have an idea?

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

Total price in constant sum CBC

Hi everyone,

I would like to show the total price in addition to the total allocated amount at the bottom of each task in a constant sum CBC.

The total price would involve a simple calculation of the ammount allocated and the price of each alternative. I have tried to solve this with some scripting, using hidden HTML objects in the price level text and Javascript in the choice task to do the calculation. Unfortunately, my Javascript skills are a bit rusty and I cannot seem to get it to work.

Any help or pointers are highly appreciated!

Thanks! G.
asked Apr 26, 2021 by gjg280 (150 points)
What version of SSI Web / Lighthouse Studio are you running?

Do your prices include decimals and, if so, what character are you using as your decimal?
Hi Zachary, I am running Lighthouse Studio 9.10.0.  I am using dots as decimal separator, but will be able to swap to comma's if that would be more convenient.

1 Answer

0 votes
Best answer
Start by adding this HTML to the label of your price attribute:

<input type="hidden" class="myPriceAttribute"/>

(You can define an internal label to keep this HTML from showing up in analysis.)

Then you can add this to wherever on the page you want to display the total price:

<span class="myTotalPrice"></span>

Finally, including this code in the CBC will do all the work we need:

$('#[% QuestionName() %]_div input').keyup(updateTotalPrice);

function updateTotalPrice() {
    var index = $('.myPriceAttribute').closest('.cbc_cell').index();
    var total = 0;
    $('#[% QuestionName() %]_div .cbc_concept:not(.none_concept)').each(function(){
        var price = Number($(this).children().eq(index).text().replace(/[^0-9\.]/g, ''));
        var quantity = Number($(this).find('.numeric_input').val());
        total += price * quantity;
    total = total.toFixed(2);
answered Apr 26, 2021 by Zachary Platinum Sawtooth Software, Inc. (215,075 points)
selected Apr 30, 2021 by gjg280
Thanks Zachary, works like a charm!