Advanced Techniques

Forsta HX Platform - API Scripting Guide

Advanced Techniques

BMI Calculator:

// AQ1: Height (cm), AQ2: Weight (kg)

/**
 * calcBMI - Calculates Body Mass Index from height and weight inputs
 * @description Uses the standard BMI formula: weight(kg) / height(m)²
 *              Converts height from cm to meters, calculates BMI,
 *              and categorizes the result (Underweight/Normal/Overweight/Obese).
 *              Displays the result in the .bmi-result element.
 */
function calcBMI() {
    // Parse height and weight as integers from the form inputs
    var h = parseInt(f(AQ1).val()); // Height in centimeters
    var w = parseInt(f(AQ2).val()); // Weight in kilograms
    
    if(h && w) {
        var bmi = (w / Math.pow(h/100, 2)).toFixed(1);
        var category = "";
        
        if(bmi < 18.5) category = "Underweight";
        else if(bmi < 25) category = "Normal";
        else if(bmi < 30) category = "Overweight";
        else category = "Obese";
        
        $('.bmi-result').html(
            'BMI: ' + bmi + ' (' + category + ')'
        );
    }
}

f(AQ1).on('change', calcBMI);
f(AQ2).on('change', calcBMI);

Dynamic Question Generation:

// Generate follow-up Qs for each symptom

f(BQ1).on('change', function() {
    var symptoms = $(this).val() || [];
    $('.dynamic-followup').remove();
    
    symptoms.forEach(function(id) {
        var name = f(BQ1).find('[value="' + id + '"]')
                         .next('label').text();
        var html = '<div class="dynamic-followup">' +
            '<h4>' + name + '</h4>' +
            '<p>How long: <input type="text" data-symptom="' + id + '"/></p>' +
            '<p>Severity: <select data-symptom="' + id + '">' +
            '<option>Mild</option>' +
            '<option>Moderate</option>' +
            '<option>Severe</option>' +
            '</select></p>' +
            '</div>';
        f(BQ1).after(html);
    });
});

⚠️ Local Storage - Euro SaaS GDPR Note:

The Forsta HX Platform handles data persistence automatically. Do not use localStorage or sessionStorage to store survey responses, as this may violate GDPR requirements in the Euro SaaS environment. The platform provides built-in save/resume functionality when configured.

If you need save/resume functionality, configure it in: Survey Designer → Settings → Save & Resume