Welcome to the LimeSurvey Community Forum

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

using expression manager codes in javascript

  • Moonlight7
  • Moonlight7's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
8 years 10 months ago - 8 years 10 months ago #139408 by Moonlight7
Hi everyone,
I have three equation type questions (aScore, bScore, cScore) and I'm trying to use javascript in a Text Display question to sort the results of the three equations in a descending order (since I couldn't find any sorting function available in EM ). I did this :
Code:
<script type="text/javascript" charset="utf-8">
   var points=["{aScore}","{bScore}","{cScore}"]; 
   points.sort(function(a,b){
   return b - a
   })
   document.write(points);
</script>

The above code works well and sorts correctly if I put an array of numbers but what I get when I use the questions codes like shown above is a reverse order of how I put the variables in the array above irrespective of their values.

e.g. assume {aScore} was 3, {bScore} 17, and {cScore} was 5 then the above script would give me : 5, 17, 3 as if the code does not recognise the variables as numbers !

Any views on what I'm missing here please ??

edit: I guess the the problem is that EM {..} are being interpreted after the javascript function ended and I hope this can be solved some way or there is a simpler solution to what I need to achieve.
Thanks so much
Last edit: 8 years 10 months ago by Moonlight7.
The topic has been locked.
More
8 years 10 months ago #139417 by Ben_V

Moonlight7 wrote: I guess the the problem is that EM {..} are being interpreted after the javascript function ended and I hope this can be solved some way or there is a simpler solution to what I need to achieve.
Thanks so much


Probably working if not "All in one" display. Try group by group or question by question presentation

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)
The following user(s) said Thank You: Moonlight7
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 10 months ago #139420 by tpartner
Replied by tpartner on topic using expression manager codes in javascript
Try removing the quotes from the array elements so they're not interpreted as strings.

Code:
var points=[{aScore}, {bScore}, {cScore}];

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Moonlight7
  • Moonlight7's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
8 years 10 months ago #139422 by Moonlight7
Replied by Moonlight7 on topic using expression manager codes in javascript
@Ben, thanks that solved it when put on a later page. I wish I could make it work on same page though.

tpartner wrote: Try removing the quotes from the array elements so they're not interpreted as strings.

Code:
var points=[{aScore}, {bScore}, {cScore}];


Thanks Tony but putting the quotes was the only way to make EM work! If I remove them the {} get stripped and no more useful !
I am just learning by trial and error ! :(
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose