- Posts: 5033
- Thank you received: 1257
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
- Topic Author
- Offline
- Platinum Member
Less
More
8 years 9 months ago #122159
by jelo
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
display the one item randomly from a multiple choice question with otherfield was created 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
Can somebody point me to a post or manual page to refresh my memories about EM.
Thanks in advance.
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
- Topic Author
- Offline
- Platinum Member
Less
More
- Posts: 5033
- Thank you received: 1257
8 years 9 months ago - 8 years 9 months ago #122164
by jelo
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Replied by jelo on topic display the one item randomly from a multiple choice question with otherfield
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 9 months ago by jelo.
The topic has been locked.
- jelo
- Topic Author
- Offline
- Platinum Member
Less
More
- Posts: 5033
- Thank you received: 1257
8 years 9 months ago #122167
by jelo
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Replied by jelo on topic display the one item randomly from a multiple choice question with otherfield
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?
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.
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)}
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.
- first
- Offline
- Elite Member
Less
More
- Posts: 227
- Thank you received: 36
8 years 9 months ago #122173
by first
Survey Designer and Programmer
Replied by first on topic display the one item randomly from a multiple choice question with otherfield
I dont know with EM. Have u tried this
manual.limesurvey.org/Workarounds:_Manip...estion_for_later_use
Survey Designer and Programmer
The topic has been locked.
- jelo
- Topic Author
- Offline
- Platinum Member
Less
More
- Posts: 5033
- Thank you received: 1257
8 years 9 months ago #122218
by jelo
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Replied by jelo on topic display the one item randomly from a multiple choice question with otherfield
Thanks for the link.
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
- Dennis
- Offline
- Junior Member
Less
More
- Posts: 21
- Thank you received: 0
8 years 8 months ago - 8 years 8 months ago #123116
by Dennis
Replied by Dennis on topic display the one item randomly from a multiple choice question with otherfield
Last edit: 8 years 8 months ago by Dennis.
The topic has been locked.
- david2013
- Offline
- Senior Member
Less
More
- Posts: 42
- Thank you received: 6
8 years 8 months ago #123118
by david2013
Replied by david2013 on topic display the one item randomly from a multiple choice question with otherfield
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.
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.
- Dennis
- Offline
- Junior Member
Less
More
- Posts: 21
- Thank you received: 0
8 years 8 months ago - 8 years 8 months ago #123122
by Dennis
Replied by Dennis on topic display the one item randomly from a multiple choice question with otherfield
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>
Attachments:
Last edit: 8 years 8 months ago by Dennis.
The topic has been locked.
- Dennis
- Offline
- Junior Member
Less
More
- Posts: 21
- Thank you received: 0
8 years 8 months ago #123260
by Dennis
Replied by Dennis on topic display the one item randomly from a multiple choice question with otherfield
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 ) {
if ( $(checkBoxes).attr('checked') == true ) {
to
if ( $(checkBoxes).prop('checked') == true ) {
The topic has been locked.