Is it possible to combine Javascript and Expression Manager?
I need to parse input data to generate a value where Javascript should come in handy.
If possible, could someone show me a simple example to demonstrate this, eg read 2 variables and add them in Javascript and use this value in the Expression Manager?
However, I am not very familiar with Javascript, I can read code and adjust it, but not start something from scratch.
Could someone show me an example of how to calculate a sum of two variables and save the value?
Isn't it the best way to save the value in an Equation question to put it in the database?
If I have a start, I think I will be able to tune it to my needs.
Thx in advance
Well, assuming you had two text or numeric questions on a previous page (q1 and q2), placing this in the source of a text question should load that question with the sum of the previous questions.
Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Define some variables
var thisQuestion = $('#question'+qID);
var q1Value = '{q1.shown}';
var q2Value = '{q2.shown}';
var sumValue = Number(q1Value) + Number(q2Value);
// Load the question
$('input[type="text"]', thisQuestion).val(sumValue);
// Hide this question
$(thisQuestion).hide();
});
</script>
Cheers,
Tony Partner Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Hi,
I think it's easier to compute scores using EM.
However, there is still a benefit of using javascript and a numerical input type question if you need some public statistics...As far as I know equations type questions are note supported
To try it:
1) create 2 numerical questions (q1 and q2)
2) create another numerical question (sum) pasting the following code in the source:
I did not know you could use EM expressions that way which makes it very powerful.
A suggestion is to put this in the manual (maybe a 'for dummies' section?).
For the same pupose, i prefer to use 'assigment' operator.
Q1 : first value
Q2 : 2nd value
NUMSTAT: numerci question type hidden
EQUA : Equation question type with : {NUMSTAT=sum(Q1,Q2)} hidden
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. -
Professional support
-
Plugins, theme and development
. I don't answer to private message.