Some assumptions:
- All questions use the same answer codes
- There is only one multiple-options question on the page with Q2
- There is only one array-numbers question on the page with Q3
- Q2 and Q3 are on page(s) following the Q1 page
Q1:
- Add a short-text to the page, lets give it a code "QHidden".
- Set Q1 to have randomized answers.
- Add the following code to one of the questions. It will hide the short text and populate it with the Q1 answer codes in the order displayed.
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Hide the short-text
$('.text-short').hide();
// The multi-opt id
var multiOptID = $('.multiple-opt').attr('id').split('question')[1];
// Populate the short-text
var ansCodesList = new Array();
$('.multiple-opt li[id^="javatbd"]').each(function(i){
var ansCode = $(this).attr('id').split('X'+multiOptID)[1];
ansCodesList.push(ansCode);
});
$('.text-short input.text').val(ansCodesList);
});
</script>
Q2:
- Add the following code to the source of Q2. It will find the order of the answer codes in Q1 and use the same order here.
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Get the previous answer order
var ansCodesList = '{QHidden.value}'.split(',');
// The multi-opt id
var multiOptID = $('.multiple-opt').attr('id').split('question')[1];
var answersParent = $('.multiple-opt li[id^="javatbd"]:eq(0)').parent();
// Change the order of the multi-opt answers
$(ansCodesList).each(function(i){
$(answersParent).append($('.multiple-opt li[id$="'+multiOptID+this+'"]'))
});
});
</script>
Q3:
- Add the following code to the source of Q3. It will find the order of the answer codes in Q1 and use the same order here. Then it will set the correct alternating background colours for the array rows.
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Get the previous answer order
var ansCodesList = '{QHidden.value}'.split(',');
// The array id
var multiOptID = $('.array-multi-flexi').attr('id').split('question')[1];
var answersParent2 = $('.array-multi-flexi tbody[id^="javatbd"]:eq(0)').parent();
// Change the order of the array rows
$(ansCodesList).each(function(i){
$(answersParent2).append($('.array-multi-flexi tbody[id$="'+multiOptID+this+'"]'))
});
// Fix up the array row background colours
var rowIndex = 0;
$('.array-multi-flexi table.question tbody tr').each(function(i, el){
rowIndex ++;
$(el).removeClass('array1, array2');
if(rowIndex % 2 == 0) {
$(el).addClass('array1');
}
else {
$(el).addClass('array2');
}
});
});
</script>