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.
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.
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.
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>