Medical Survey Examples

Forsta HX Platform - API Scripting Guide

Medical Survey Examples

Example 1: Patient Demographics

// AQ1: Age, AQ2: Gender, AQ3: Chronic conditions?

f(AQ3).on('change', function() {
    if($(this).val() == "1") {
        f(AQ3a).show(); // Which conditions
        f(AQ3b).show(); // Duration
    } else {
        f(AQ3a).hide().val("");
        f(AQ3b).hide().val("");
    }
});

Example 2: Symptom Assessment

// BQ1: Symptoms (multiple), BQ2: Severity, BQ3: Duration

f(BQ1).on('change', function() {
    var symptoms = $(this).val();
    if(symptoms && symptoms.length > 0) {
        f(BQ2).show();
        f(BQ3).show();
        
        // Pipe selected symptoms
        var list = symptoms.map(function(id) {
            return f(BQ1).find('[value="' + id + '"]')
                         .next('label').text();
        }).join(', ');
        
        f(BQ2).find('.question-text').html(
            "Rate severity of: " + list
        );
    } else {
        f(BQ2).hide().val("");
        f(BQ3).hide().val("");
    }
});

Example 3: Medication History

// CQ1: Taking medications?, CQ2: List them

var medCount = 0;

f(CQ1).on('change', function() {
    if($(this).val() == "1") {
        addMedication();
    } else {
        $('.med-entry').remove();
        medCount = 0;
    }
});

/**
 * addMedication - Dynamically adds a medication entry form
 * @description Creates HTML for a new medication entry with:
 *              - Medication name input
 *              - Dosage input
 *              - Frequency dropdown (Daily/Twice daily/Weekly)
 *              Increments medCount to number entries sequentially.
 */
function addMedication() {
    medCount++; // Increment counter for unique numbering
    var html = '<div class="med-entry">' +
        '<h4>Medication ' + medCount + '</h4>' +
        '<input type="text" placeholder="Name" class="med-name"/>' +
        '<input type="text" placeholder="Dosage" class="med-dose"/>' +
        '<select class="med-freq">' +
        '<option>Daily</option>' +
        '<option>Twice daily</option>' +
        '<option>Weekly</option>' +
        '</select>' +
        '</div>';
    f(CQ2).after(html);
}

Example 4: Pain Scale

// DQ1: Pain level (0-10)

f(DQ1).on('input change', function() {
    var level = parseInt($(this).val());
    var desc, color;
    
    if(level <= 2) {
        desc = "Minimal"; color = "#28a745";
    } else if(level <= 6) {
        desc = "Moderate"; color = "#ffc107";
    } else {
        desc = "Severe"; color = "#dc3545";
    }
    
    $(this).next('.pain-feedback').remove();
    $(this).after(
        '<div class="pain-feedback" style="color:' + color + '">' +
        desc + ' pain</div>'
    );
});

Example 5: Appointment Scheduling

// EQ1: Schedule follow-up?, EQ2: Date, EQ3: Time

f(EQ1).on('change', function() {
    if($(this).val() == "1") {
        f(EQ2).show();
        f(EQ3).show();
        
        // Set min date to tomorrow
        var tomorrow = new Date();
        tomorrow.setDate(tomorrow.getDate() + 1);
        f(EQ2).find('input').attr('min',
            tomorrow.toISOString().split('T')[0]
        );
    } else {
        f(EQ2).hide().val("");
        f(EQ3).hide().val("");
    }
});

// Validate business hours
f(EQ3).on('change', function() {
    var hour = parseInt($(this).val().split(':')[0]);
    if(hour < 9 || hour >= 17) {
        alert("Select 9 AM - 5 PM");
        $(this).val("");
    }
});

Example 6: Quality of Life Score

/**
 * Quality of Life (QOL) Score Calculator
 * @description Calculates average score from 10 QOL questions (FQ1-FQ10)
 *              using a 1-5 scale. Updates display with both raw average
 *              and percentage score.
 */

// Array of all QOL question IDs
var qolQs = ['FQ1','FQ2','FQ3','FQ4','FQ5',
             'FQ6','FQ7','FQ8','FQ9','FQ10'];

// Attach change event to all QOL questions to recalculate on any change
qolQs.forEach(function(qid) {
    f(qid).on('change', calcQOL);
});

/**
 * calcQOL - Calculates and displays the Quality of Life score
 * @description Loops through all QOL questions, sums valid numeric responses,
 *              calculates average (total/count), converts to percentage,
 *              and updates the .qol-score display element.
 */
function calcQOL() {
    var total = 0, count = 0;
    
    qolQs.forEach(function(qid) {
        var val = parseInt(f(qid).val());
        if(!isNaN(val)) {
            total += val;
            count++;
        }
    });
    
    if(count > 0) {
        var avg = (total / count).toFixed(1);
        var pct = ((avg / 5) * 100).toFixed(0);
        $('.qol-score').html(
            '<strong>QOL Score:</strong> ' + 
            avg + '/5.0 (' + pct + '%)'
        );
    }
}