Have an idea?

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

java script in ACBC unacceptables

Hi, I would like to run java script in ACBC unacceptable questions to modify the price that is presented to the respondent.
I have the script in the footer of unacceptable questions. the script seems to be fine, but has no impact on the final price presented to the respondent.

                       jquery(document).ready(function ($) {
                                    var price_change = $('td.level_text_cell div').innerHTML.match(/<!--.*?-->/g);
                                    if (price_change) price_change = ("" + price_change + "").replace(/^.*?<!--(.*?)-->$/m, '$1');
                                    price_change = parseInt(price_change, 10);
            
                                    var price_div = $("span.cena");
            
                                    var price = price_div.innerHTML.replace(/^\D*(.*?)$/m, '$1');
                                    price = parseInt(price.replace(/\,/, ''), 10);
            
                                    var new_price = price + price_change;
                                    if (new_price < 0) new_price = 0;
                                    if (!isNaN(new_price)) price_div.innerHTML = " " + new_price + ".99";
            });
</script>

What should I do to make the script work in unacceptable questions?
asked Mar 20 by RafalNeska (450 points)
I'm not sure I have all the information here.  There's a "span.cena" element that has the original price, but you want to add in some additional prices from HTML comments elsewhere on the page it seems.  Where are these HTML comments coming from?  Are they from the level labels of the other items being shown in the Unacceptable?

On a tangential note, you may also want to consult with someone with more conjoint experience on doing this.  If you alter the shown price in the Unacceptable, the Simulator will still think the respondent saw the original price, potentially impairing the integrity of your results.
Hi Zachary,
thank you for your comment.

Below you can see HTML including additional price. It is not related to any levels, it is in Header 2.
I am aware that the orginal price is recorded in the data file.

<table><tr><td><div class=\"label_text\"><!-- <b>Internet</b>: --></div></td><td class=level_text_cell inner_table_cell alt_color2><div class=\"level_text\"><!-- 20 --></div></td></tr>

<script>
                                   jquery(document).ready(function ($) {
                                    var price_change = $('td.level_text_cell div').innerHTML.match(/<!--.*?-->/g);
                                    if (price_change) price_change = ("" + price_change + "").replace(/^.*?<!--(.*?)-->$/m, '$1');
                                    price_change = parseInt(price_change, 10);
            
                                    var price_div = $("span.cena");
            
                                    var price = price_div.innerHTML.replace(/^\D*(.*?)$/m, '$1');
                                    price = parseInt(price.replace(/\,/, ''), 10);
            
                                    var new_price = price + price_change;
                                    if (new_price < 0) new_price = 0;
                                    if (!isNaN(new_price)) price_div.innerHTML = " " + new_price + ".99";
            });
</script>
</div>
<div class="question_body">
<table class="inner_table" width="100%"><tr><td class="center_panel_cell"><div class="acbc_potential_rules">
<table class="acbc_rules"><tr class="clickable"><td class="input_cell" width="5%">
 <span class="test_label test_var_label">tel1_Unacceptable4=1</span> <div id="tel1_Unacceptable4_1_graphical" class="graphical_select radiobox"></div><input name="tel1_Unacceptable4" value="1" type="radio" id="tel1_Unacceptable4_1" class="HideElement">
</td><td class="option_cell">
<div class="options">
<label for="tel1_Unacceptable4_1">
<b>Abonament mies. netto [bez VAT]</b>:  Większy niż<span class="cena"> 49.99</span>&nbsp;zł
/ mies.</label>
</div>
</td></tr>

1 Answer

0 votes
 
Best answer
I think we could lessen the amount of work we have to do if we replace the HTML comment with a hidden input like this:

<input type="hidden" class="additional_price" value="20"/>


Then we can add it to the span with this JavaScript:

$(document).ready(function(){
    var priceElement = $('span.cena');
    var originalPrice = parseInt($(priceElement).text().replace(/,/g, ''));
    var additionalPrice = parseInt($('.additional_price').val());
    var totalPrice = originalPrice + additionalPrice;
    if (totalPrice < 0) {
        totalPrice = 0;
    }
    if (!isNaN(totalPrice)) {
        $(priceElement).text(' ' + totalPrice + '.99');
    }
})
answered Mar 20 by Zachary Platinum Sawtooth Software, Inc. (120,175 points)
selected Mar 21 by RafalNeska
Zachary, many thanks:)
...