Welcome to the LimeSurvey Community Forum

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

Store Value of JS-Variable in hidden Question

  • hfi_jcc
  • hfi_jcc's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 2 months ago #193752 by hfi_jcc
Hey,
My survey is currently using JS to calculate some values. I am now trying to store those values in a hidden question using the EM for further use. The actual script is quite complex, but it boils down to something like this:

let someVariable = "something";
{SomeQuestionCode = someVaribale}

Unfortunately this doesnt work, as the expressions are evaluated before the script runs (i assume).
Is there a way to accomplish this?

I guess making all my calculations using only the EM would technically work, but I already have the JS working and ~100lines of js transformed to EM would probably end but being quite a mess.

Thanks!
The following user(s) said Thank You: Indispirit
The topic has been locked.
More
4 years 2 months ago #193754 by Indispirit
Replied by Indispirit on topic Store Value of JS-Variable in hidden Question
This is something I'd really like to be able to do as well, as AFAIK, expression manager doesn't have an easy way to sort answers.

Seem to be two or three questions on the forum where people ask how they could select say the top highest scoring answers using a sort and then present them back to the user at the survey end.

Currently the best way to do this seems to be via javascript and hidden questions but that means you can't save the answers.

One approach I was considering is that I gather LS makes a temporary instance of the answers before they are stored in database on submit that you could write a function to overwrite but that could lead to ugliness.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 2 months ago #193757 by Joffm
Hi,

without seeing more of your approach I should say:

Your script ends like this:
Code:
$('#question{QID} input[type="text"]').val(myCalculatedValue)

So in this question the value is stored.
And you hide this question not by LS, but by
Code:
$('#question{QID}').hide();

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: hfi_jcc
The topic has been locked.
More
4 years 2 months ago #193758 by jelo

Indispirit wrote: hidden questions but that means you can't save the answers.

LimeSurvey is not hiding the question but removing the question from the HTML-Code.
This was always confusing for users.

I have a featurerequest to show a more advanced "hiding" feature inside the GUI.
bugs.limesurvey.org/view.php?id=11665
That would raise awareness of the differences.
Hiding via Javascript (as Joffm) showed is the way to go.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The following user(s) said Thank You: Indispirit
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 2 months ago #193762 by tpartner
Replied by tpartner on topic Store Value of JS-Variable in hidden Question
Or hide it with a CSS class "hidden".

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: Indispirit
The topic has been locked.
More
4 years 2 months ago - 4 years 2 months ago #193764 by Indispirit
Replied by Indispirit on topic Store Value of JS-Variable in hidden Question
Thanks, really useful. With this approach, if you are on the final page of the survey before submit, how could your hidden question access the values of any answers not yet submitted (ie earlier on the page), eg in the example below q9 and q10?:

[page 1-4]
questions 1-8
[/page 1-4]

[page 5]
q9
q10
val(myCalculatedValue)= sum q1-10
$('#question{QID} input[type="text"]').val(myCalculatedValue)
{submit}
[/page 5]
Last edit: 4 years 2 months ago by Indispirit.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 2 months ago #193766 by tpartner
Replied by tpartner on topic Store Value of JS-Variable in hidden Question
Why not use a hidden equation question instead of loading a text question?

Code:
{sum(q1, q2, q3, q4, q5, q6, q7, q8, q9, q10)}

Small sample survey attached:

File Attachment:

File Name: limesurvey...2-10.lss
File Size:32 KB

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
4 years 2 months ago - 4 years 2 months ago #193769 by Indispirit
Replied by Indispirit on topic Store Value of JS-Variable in hidden Question
Would definitely work it was just a summation but val(myCalculatedValue)= sum q1-10 was just an example.

Instead we have a JS equation that does the following:
Pairwise summation of questions {equation question could do this}
Bubblesort {not possible with equation editor?}
Select between ties with weights {requires loops, not possible with equation question?}
Choose top three answers

Afaik, that's not easy to implement with the equation question so we've gone with JS, hence the question about accessing answer values before submission.
Last edit: 4 years 2 months ago by Indispirit.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 2 months ago - 4 years 2 months ago #193775 by tpartner
Replied by tpartner on topic Store Value of JS-Variable in hidden Question
Well, you could interrupt the submit process something like this but if some of the questions are on that page the Expression Manager result(s) may be wrapped in a <span> element so you will have to parse it out of that.

Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready', function(){    
 
    // Interrupt the submit
    $('#limesurvey').on('submit', function(e) {
 
      // Load the hidden input
      var myCalculatedValue = "{sum(q1, q2, q3, q4, q5, q6, q7, q8, q9, q10)}";
      $('#question{QID} input:text').val(myCalculatedValue);
    });
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 4 years 2 months ago by tpartner.
The following user(s) said Thank You: Indispirit
The topic has been locked.
More
4 years 2 months ago #193799 by Indispirit
Replied by Indispirit on topic Store Value of JS-Variable in hidden Question
Thanks Tony, good suggestion, will look into it.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose