Have an idea?

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

Change BYO prices (right column) with js

Hello all,

in an ACBC I would like to localize the amount shown in the "cost for feature" column (right most column).  I want the "underlying" values to not change (for modelling purposes in the back-end I need all respondents to use the same currency), so it's really only to display local values.  I am fine with the level cost showing in the actual BYO question are in $ (the "(+ $xxx) shown next to level text.

My script works when all attributes are shown.  But the attribute list is dynamic meaning some attributes can be left out and then I run into the issue that some cells are "undefined" and the js stops running.

My code (only showing 2 attributes)

<code>
$(document).on("ssi_ready", function(){

$('select').change(function(){

var price1 = parseInt($('#ACBC1_BYO_price1').val().replace(',',''));    
var local1 = $.number(price1 * [% Value(multiplier) %],0,".",",");
$('#ACBC1_BYO_price1').val(local1);

var price2 = parseInt($('#ACBC1_BYO_price2').val().replace(',',''));    
var local2 = $.number(price2 * [% Value(multiplier) %],0,".",",");
$('#ACBC1_BYO_price2').val(local2);


var price = parseInt($('#ACBC1_BYO_13').val().replace(',',''));    
var local = $.number(price * [% Value(multiplier) %],0,".",",");
$('#ACBC1_BYO_14').val(local);

})
});
</code>

The 3rd set is to update the total price shown at the bottom of the BYO.

So this works when both attributes 1 and 2 are in the ACBC.  But if attribute 1 is dropped from the ACBC, then I get the "undefined" error since #ACBC1_BYO_price1 does not exist on the page.

I tried defining variable differently but could not get this to work.

Any idea?
asked 6 days ago by amic Bronze (1,310 points)

1 Answer

+1 vote
It looks like you're real close here.  It seems like what we need to do is tell the JavaScript to first check whether a given price element exists, then only run the rest of the code for that price if it was found.  There are a few ways we could do this, but the simplest might be to check the "length" property on the jQuery element.  This should return 1 (truthy) if the element was found or 0 (falsey) if the element was not found.  Check out this example:

var element1 = $('#ACBC1_BYO_price1');
if ($(element1).length) {
    var price1 = parseInt($(element).val().replace(',', ''));    
    var local1 = $.number(price1 * [% Value(multiplier) %], 0, ".", ",");
    $(element1).val(local1);
}


On a tangential note, you may be interested in looking into "Multi-Currency ACBC" from the Community Question Library for this or future projects.  You could set one price for each "multiplier" value that you have, then use "multiplier" (or a related survey variable) to only show one price to each respondent.  You can download it from here:

https://sawtoothsoftware.com/resources/question-library/multi-currency-acbc

I know it may be a lot of settings to take in at first glance; that was just necessary to accommodate all the different possible settings in ACBC.  I'd be happy to answer any questions that might arise from using this tool.
answered 6 days ago by Zachary Platinum Sawtooth Software, Inc. (171,950 points)
Thanks Zachary that works.  I was not aware of your multi currency acbc script, looks awesome and will definitely check it out / use it.

Cheers,
amic
...