Survey.StylesManager.applyTheme("orange");
function setCapitalAndCurrency(params) {
if (params.length < 2) return;
var country = params[0];
var capitalQuestionName = params[1];
var currencyQuestionName = params.length > 2 ? params[2] : null;
var survey = this.survey;
if (!country || !country.length) {
survey.clearValue(capitalQuestionName);
if (!!currencyQuestionName) {
survey.clearValue(currencyQuestionName);
}
return;
}
survey.setVariable("request_processing", true);
survey.setVariable("request_error", null);
country = country.toLowerCase();
$.ajax({
url: "https://restcountries.eu/rest/v2/name/" + country,
type: "GET",
success: function (data) {
if (!data || data.length < 1) return;
var countryValue = data[0];
survey.setValue(capitalQuestionName, countryValue.capital);
var currencyValue = countryValue.currencies.length > 0 ? countryValue.currencies[0] : {};
if (!!currencyQuestionName) {
survey.setValue(currencyQuestionName, JSON.stringify(currencyValue));
}
survey.setVariable("request_processing", false);
},
error: function (xhr, ajaxOptions, thrownError) {
survey.setVariable("request_error", "The country is not found.");
survey.setVariable("request_processing", false);
survey.clearValue(capitalQuestionName);
if (!!currencyQuestionName) {
survey.clearValue(currencyQuestionName);
}
}
});
}
Survey.FunctionFactory.Instance.register("setCapitalAndCurrency", setCapitalAndCurrency);
var json = {
questions: [
{ type: "text", name: "country", title: "Please enter the country" },
{ type: "html", name: "requesting", html: "The data is requesting", visibleIf: "{request_processing} = true" },
{ type: "text", name: "capital", title: "Capital of {country} is:", readOnly: true, visibleIf: "{capital} notempty" },
{ type: "text", name: "currency", title: "Currency in {country} is:", readOnly: true, visibleIf: "{currency} notempty" },
{ type: "html", name: "error", html: "The following error has happened: <b>{request_error}</b>", visibleIf: "{request_error} notempty" },
],
triggers: [
{ type: "runexpression", expression: "{country} notempty or {country} empty", runExpression: "setCapitalAndCurrency({country}, 'capital', 'currency')" }
]
};
window.survey = new Survey.Model(json);
survey.onComplete.add(function(result) {
document.querySelector('#surveyResult').textContent =
"Result JSON:\n" + JSON.stringify(result.data, null, 3);
});
$("#surveyElement").Survey({
model: survey
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>On changing an answer, you may run an expression, jQuery Survey Library Example</title>
<meta name="viewport" content="width=device-width" />
<script src="https://unpkg.com/jquery"></script>
<script src="/DevBuilds/survey-jquery/survey.jquery.min.js"></script>
<link href="/DevBuilds/survey-knockout/survey.min.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div id="surveyElement" style="display:inline-block;width:100%;">
</div>
<div id="surveyResult"></div>
<script type="text/javascript" src="./index.js"></script>
</body>
</html>