Welcome to the LimeSurvey Community Forum

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

Javascript with Version 4.2.1

  • mbarbalas20
  • mbarbalas20's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #198444 by mbarbalas20
Javascript with Version 4.2.1 was created by mbarbalas20
We are interested in using Javascript in our survey to set the default answers to "0" for an Array (numbers) type of question.

We looked at the documentation ( manual.limesurvey.org/Workarounds:_Manip...ime_using_Javascript ) and tried the plugin recommended. We got an error message that this plugin is "not compatible with your version of Limesurvey".

Is there another way to enable Javascript within Version 4.2.1?
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #198456 by Joffm
Replied by Joffm on topic Javascript with Version 4.2.1
Hi, unfortunately your link doesn't show the exact snippet.

But the one I found said "tested with 2.05"
Of course you cannot expect it to work in 4.x.

We are interested in using Javascript in our survey to set the default answers to "0" for an Array (numbers) type of question.

Is there a special reason that you want to use javascript?
You can do it easily by ExpressionScript.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago - 3 years 11 months ago #198477 by tpartner
Replied by tpartner on topic Javascript with Version 4.2.1
In version 4.x, assuming you have the correct permissions, there is an editor field where you can enter JavaScript directly when editing questions.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 11 months ago by tpartner.
The topic has been locked.
  • christianlangkamp
  • christianlangkamp's Avatar
  • Offline
  • Junior Member
  • Junior Member
More
3 years 8 months ago #204163 by christianlangkamp
Replied by christianlangkamp on topic Javascript with Version 4.2.1
Is there a different way to Javascript to set the default answer to 0 and have it prefilled ? There was some years back exactly this thread, but that was locked, so apologies for reposting.

I have a classical array, not array by numbers (even though the answer code is numbers), but it would be great to simply have the 'not interested' field prefilled.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #204172 by tpartner
Replied by tpartner on topic Javascript with Version 4.2.1
1) This is not relevant to this topic. Please start a new topic.

2) Always attach survey exports (.lss) files, not question exports. The volunteers here should not have to spend time creating surveys to test your problems.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • christianlangkamp
  • christianlangkamp's Avatar
  • Offline
  • Junior Member
  • Junior Member
More
3 years 8 months ago #204173 by christianlangkamp
Replied by christianlangkamp on topic Javascript with Version 4.2.1
First of all - apologies.

"We are interested in using Javascript in our survey to set the default answers to "0" for an Array (numbers) type of question."

I thought this was essentially still the old topic.

Secondly, sure, I thought it was easier to import a test question into an existing survey than set up a complete survey.

Can also extract survey of course.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #204174 by tpartner
Replied by tpartner on topic Javascript with Version 4.2.1
No, let me apologize, I didn't read back far enough.

Please attach a survey export (.lss file) containing only the relevant question.

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: christianlangkamp
The topic has been locked.
  • christianlangkamp
  • christianlangkamp's Avatar
  • Offline
  • Junior Member
  • Junior Member
More
3 years 8 months ago #204175 by christianlangkamp
Replied by christianlangkamp on topic Javascript with Version 4.2.1
So the default setting should be 'No Interest', i.e. 0.

And the reader only then requested to shift the items relevant to her/him.

Thanks, and thanks for the instructions on how to best post in the forum.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #204178 by tpartner
Replied by tpartner on topic Javascript with Version 4.2.1
This will set a default answer of "0" in an array question:

Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    var defaultCode = 0;
 
    // Loop through the rows
    $('tr.answers-list', thisQuestion).each(function(i) {
      var thisRow = $(this);
 
      // Set default answer
      if($('input:radio:checked', thisRow).length == 0) {
        $('input:radio[value="'+defaultCode+'"]', thisRow).trigger('click');
      }
    });
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...9(1).lss
File Size:22 KB

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: christianlangkamp
The topic has been locked.
  • christianlangkamp
  • christianlangkamp's Avatar
  • Offline
  • Junior Member
  • Junior Member
More
3 years 8 months ago #204179 by christianlangkamp
Replied by christianlangkamp on topic Javascript with Version 4.2.1
Hi - Confirmed- works like a charm.


On a questionnaire with 200 question out of which only 10 are expected to be picked, this is a major major help !
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose