Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Combine EM and Javascript

More
10 years 11 months ago #113021 by pvdemael
Hi all,

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?

Thx

P
The topic has been locked.
More
10 years 11 months ago #113050 by tpartner
Replied by tpartner on topic Combine EM and Javascript
If it came down to that I would use pure JS.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
10 years 11 months ago #113105 by pvdemael
Replied by pvdemael on topic Combine EM and Javascript
Thx Tony,

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
The topic has been locked.
More
10 years 11 months ago - 10 years 11 months ago #113142 by tpartner
Replied by tpartner on topic Combine EM and Javascript
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.
Last edit: 10 years 11 months ago by tpartner.
The following user(s) said Thank You: pvdemael
The topic has been locked.
More
10 years 11 months ago - 10 years 11 months ago #113157 by Ben_V
Replied by Ben_V on topic Combine EM and Javascript
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:
Code:
<script type="text/javascript" charset="utf-8">  
$(document).ready(function() {
  var q1Value = {q1.shown};
  var q2Value = {q2.shown};
        var sumValue = Number(q1Value) + Number(q2Value);
 
        $(".numeric:eq(0) input.text").val(sumValue)
 
     // Hide + autosubmit 
     // $('.numeric).hide();
     // document.limesurvey.submit();
 
});
</script>

/!\ Set your survey question by question (or group by group if the "sum" question is placed in another questiongroup)


.

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Last edit: 10 years 11 months ago by Ben_V.
The following user(s) said Thank You: pvdemael
The topic has been locked.
More
10 years 10 months ago #113161 by pvdemael
Replied by pvdemael on topic Combine EM and Javascript
Hi Ben_V and Tony,

This is what I need!

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?).
The topic has been locked.
More
10 years 10 months ago - 10 years 10 months ago #113184 by DenisChenu
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.
Last edit: 10 years 10 months ago by DenisChenu.
The following user(s) said Thank You: Ben_V, pvdemael
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose