Have an idea?

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

how to show specific value in <span> on radio button selection

Hi,

i want to show specific value in <span> on radio button selection, i.e

value 1 for Radio-1
value 4 for Radio-2
value 6 for Radio-3
value 9 for Radio-4

i m using below mention code, but problem is when i go next and come back the value reset to 0

code is

<span id="total">0</span>
 
<script>
var checkboxValues = [100, 200, 300, 400];
 
function SSI_CustomGraphicalRadiobox(graphicalObj, inputObj, bln) {
    updateTotal();
}
 
function updateTotal() {
    var total = 0;
     
    for (var i = 1; i <= 4; i++) {
        if (SSI_GetValue('[% QuestionName() %]') >= i) {
            total = checkboxValues[i - 1];
        }
    }
     
    $('#total').text(total);
}
</script>


thanks in advance
asked Jun 27 by Nouman Saeed Bronze (995 points)

1 Answer

+1 vote
 
Best answer
SSI_CustomGraphicalRadiobox handles running updateTotal whenever a graphical radio button is clicked on.  Now we just need to add some code that will trigger updateTotal when the page loads up:

$(document).ready(function(){
    updateTotal();
})


On a tangential note, there may be an efficiency improvement we can make to lines 13 - 17.  This for loop is going to run once for every item in the array, running SSI_GetValue everytime and potentially setting total multiple times.  We could lessen the code and improve performance by replacing these lines with this:

total = checkboxValues[SSI_GetValue('[% QuestionName() %]') - 1];
answered Jun 27 by Zachary Platinum Sawtooth Software, Inc. (123,625 points)
selected Jun 27 by Nouman Saeed
...