Welcome to the LimeSurvey Community Forum

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

display the one item randomly from a multiple choice question with otherfield

  • jelo
  • jelo's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
8 years 8 months ago #122159 by jelo
Is the a proven way to display the text of one chosen item of the multiple choice question (including other field).

When trying to first count the number of choosen items I already seem to be unable to count the ticked other field.
questioncode_other.NAOK == 'Y' seems to be wrong for others. The fixed items are counted correctly.

with
Code:
sum(questioncode_1.NAOK == 'Y', questionscode_2.NAOK == 'Y', ....

Can somebody point me to a post or manual page to refresh my memories about EM.
Thanks in advance.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • jelo
  • jelo's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
8 years 8 months ago - 8 years 8 months ago #122164 by jelo
count{that.questioncode.NAOK} is doing the trick for counting.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Last edit: 8 years 8 months ago by jelo.
The topic has been locked.
  • jelo
  • jelo's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
8 years 8 months ago #122167 by jelo
To bring this thread down to one question:
How to select randomly one element out of the answerlist of a multiple choice question to display it in a question text?
Code:
{list(questioncode.shown)} 
I had to modify the generated em code to add the entered text of the other field.

But I haven't found a way to extract one element from list or to get to access one answered question item randomly.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
More
8 years 8 months ago #122173 by first

Survey Designer and Programmer
The topic has been locked.
  • jelo
  • jelo's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
8 years 8 months ago #122218 by jelo
The topic has been locked.
More
8 years 7 months ago - 8 years 7 months ago #123116 by Dennis
Does this solution still work in version 2.05+ (Build 150520)?

I can't get it to work :(
Last edit: 8 years 7 months ago by Dennis.
The topic has been locked.
More
8 years 7 months ago #123118 by david2013
If using EM to extract random one element from selected multiple choice, I don't think we have any existing function to do that. You will have to extend the EM by writing your own custom EM function. This is assume you can access PHP files to the server.

If using jQuery, it's better to re-write the function in the workaround section because the workaround have a lot of hardcode. It should be easy. {list(that.QMultiChoice.shown) } should give you selected choices. Then just randomize the list and return the first element.
The topic has been locked.
More
8 years 7 months ago - 8 years 7 months ago #123122 by Dennis
This workaround doesn't use EM, it's just a simple javascript. However, I must have made a mistake or something is different in the newer version of LS. Javascript is working, tripe-checked the names... ?
Code:
Which car(s) do you own? <script type="text/javascript" charset="utf-8">
 
        $(document).ready(function() {
                //// Identify the hidden question input
                //// EDIT HERE (1) - replace with your hidden question input ID
                var hiddenInput = 'answer​#119459X725X11680';
 
                //// Identify the checkboxes to be randomized
                //// EDIT HERE (3) - add or remove as required, 
                //// replace with your checkbox IDs
                var CB1 = '#answer119459X725X11664A';
                var CB2 = '#answer119459X725X11664B';
                var CB3 = '#answer119459X725X11664C';
 
                //// Create an array of all checkboxes to be randomized
                //// EDIT HERE (4) - add or remove CBs as required
                checkBoxes = new Array( CB1, CB2, CB3 );
                var checkBoxesLength = checkBoxes.length;
 
                //// The randomizing function - no editing required
                function randomResponse () {
                        var boxLabels = new Array();
 
                        // loop through all of the checkboxes and if they are checked, 
                        // add their label to the boxLabels array
                        for( var i=0; i<checkBoxesLength; i++ ) {
                                if ( $(checkBoxes[i]).attr('checked') == true ) {
                                        var cbid = checkBoxes[i].replace('#','');
                                        boxLabels.push( $( 'label[for=' + cbid + ']' ).html() );
                                }
                        }
 
                        // find a random index of the responses array
                        var randomNum = Math.floor(Math.random() * boxLabels.length); 
 
                        // populate the hidden question input with the 
                        // random input value
                        $(hiddenInput).val( boxLabels[randomNum] ); 
                }
 
                //// The listeners that will fire the randomize function if a 
                //// change occurs in the checkboxes
                //// EDIT HERE (5) - add or remove CBs as required
                $(CB1).change(function() {
                        randomResponse ();
                });
                $(CB2).change(function() {
                        randomResponse ();
                });
                $(CB3).change(function() {
                        randomResponse ();
                });
 
        });
</script>
Last edit: 8 years 7 months ago by Dennis.
The topic has been locked.
More
8 years 7 months ago #123260 by Dennis
For future references: the workaround works, but for later versions of LimeSurvey change this line:

if ( $(checkBoxes).attr('checked') == true ) {

to

if ( $(checkBoxes).prop('checked') == true ) {
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose