Welcome to the LimeSurvey Community Forum

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

Unable to export JavaScript-generated values for randomized conjoint attributes

  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 days 8 hours ago #270527 by RitaShen
Please help us help you and fill where relevant:
LimeSurvey version: [see right hand bottom of your LimeSurvey admin screen]6.14.0
Own server or LimeSurvey Cloud:LimeSurvey Cloud
Survey theme/template:bootswatch
==================Dear LimeSurvey team,I’m designing a conjoint experiment using LimeSurvey (Cloud version 6.14), and I’ve run into a serious issue regarding data export of randomized values.Here’s what I need the system to do:
 ✅ 1. Randomize and record attribute valuesEach respondent sees two policy proposals (Option A and Option B), each with six attributes. The content of these attributes is randomly selected using JavaScript upon loading the page.I use JavaScript to generate these random values and then store them into 12 hidden questions:
  • Code:
    codeA1
    to
    Code:
    codeA6
    → for Option A’s six attributes
  • Code:
    codeB1
    to
    Code:
    codeB6
    → for Option B’s six attributes
  • plus one more hidden variable
    Code:
    attorder1
    to record the randomized order of attributes shown on the screen.
These hidden variables must be exported with the response data for later analysis.
 ✅ 2. Ensure Option A and B are not identicalTo avoid showing identical options to the respondent, I use a
Code:
do...while
loop in JavaScript to ensure that Option A and B do not have exactly the same combination of attribute values.Here’s an example of the logic:
Code:
// Attributes for Option A
window.random61 = Math.floor(Math.random() * 3) + 1;
window.random63 = Math.floor(Math.random() * 4) + 1;
// ...
 
// Attributes for Option B – must differ from A
do {
  window.random62 = Math.floor(Math.random() * 3) + 1;
  window.random64 = Math.floor(Math.random() * 4) + 1;
  // ...
} while (
  window.random61 === window.random62 &&
  window.random63 === window.random64 &&
  // ...
);
 
// Store values
LSvar.codeA1 = window.random61;
LSvar.codeA2 = window.random63;
// ...
LSvar.codeB1 = window.random62;
LSvar.codeB2 = window.random64;
// ...
LSvar.attorder1 = randomizedOrderString;

❌ The problem:Although everything works in the browser —
✔️ The JavaScript executes
✔️ The
Code:
LSvar.codeA1
,
Code:
codeB1
, etc. are correctly populated (confirmed in the browser console)✔️ The hidden short-text questions exist in the same page, and are selected in the export options→ The exported response data does not contain the actual values for these 13 variables — they are all blank in the CSV output.

✔️ The hidden short-text questions exist in the same page, and are selected in the export options→ The exported response data does not contain the actual values for these 13 variables — they are all blank in the CSV output.
Is there any way to solve my problem? 

File Attachment:

File Name: limesurvey... (2).lss
File Size:199 KB

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 days 5 hours ago #270531 by tpartner
How are the questions hidden?

Un-hide them for testing to see if they are being populated.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 days 3 hours ago #270534 by RitaShen
Since I don’t want respondents to see these values during the survey, the questions are hidden. However, I still need to capture and export the actual attribute values that were displayed to each respondent. That’s why I chose to insert the JavaScript code directly into the question text.
Code:
<script type="text/javascript">
$(document).ready(function () {
    // 方案 A 屬性
    window.random61 = Math.floor(Math.random() * 3) + 1;
    window.random63 = Math.floor(Math.random() * 4) + 1;
    window.random65 = Math.floor(Math.random() * 4) + 1;
    window.random67 = Math.floor(Math.random() * 5) + 1;
    window.random69 = Math.floor(Math.random() * 3) + 1;
    window.random71 = Math.floor(Math.random() * 3) + 1;
 
    // 方案 B 屬性(確保整組不同)
    do {
        window.random62 = Math.floor(Math.random() * 3) + 1;
        window.random64 = Math.floor(Math.random() * 4) + 1;
        window.random66 = Math.floor(Math.random() * 4) + 1;
        window.random68 = Math.floor(Math.random() * 5) + 1;
        window.random70 = Math.floor(Math.random() * 3) + 1;
        window.random72 = Math.floor(Math.random() * 3) + 1;
    } while (
        window.random61 === window.random62 &amp;&amp;
        window.random63 === window.random64 &amp;&amp;
        window.random65 === window.random66 &amp;&amp;
        window.random67 === window.random68 &amp;&amp;
        window.random69 === window.random70 &amp;&amp;
        window.random71 === window.random72
    );
 
    // ✅ 寫入 hidden 題目(randomXX 值)
LSvar.codeA1 = window.random61;
LSvar.codeA2 = window.random63;
LSvar.codeA3 = window.random65;
LSvar.codeA4 = window.random67;
LSvar.codeA5 = window.random69;
LSvar.codeA6 = window.random71;
LSvar.codeB1 = window.random62;
LSvar.codeB2 = window.random64;
LSvar.codeB3 = window.random66;
LSvar.codeB4 = window.random68;
LSvar.codeB5 = window.random70;
LSvar.codeB6 = window.random72;
    // ✅ 記錄屬性順序
    const rowKeys = ['military', 'budget', 'source', 'usage', 'allies'];
    const $rows = $('#conjoint-table tbody').children('tr');
    const $middleRows = $rows.filter('.shuffle-target').get();
    const shuffled = $middleRows.sort(() => Math.random() - 0.5);
    const attrOrder = shuffled.map((_, i) => rowKeys[i]).join('|');
// ✅ 寫入 hidden 題目(屬性順序)
LSvar.attorder1 = attrOrder;
 
    // ✅ 重新排列表格順序
    const $firstRow = $rows.eq(0);
    const $lastRow = $rows.last();
    $('#conjoint-table tbody').empty().append($firstRow).append(shuffled).append($lastRow);
});

This script is intended to populate the hidden questions shown in the screenshot below. However, I’m not sure whether I implemented it correctly, as the values are missing from the exported dataset. Any guidance would be appreciated.
 
 

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 days 3 hours ago #270536 by tpartner
I'm not in a position to test your code.

If you hide the questions with the question settings, they will not be rendered in the DOM, so cannot be manipulated via JavaScript.

Hide them with CSS.

But I say again, for testing, leave them visible.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 days 35 minutes ago - 2 days 21 hours ago #270538 by Joffm
Hi,
here I used a question of type "multiple short text" to store the rendom numbers (A1-A6, B1-B6)
Code:
<script type="text/javascript">
$(document).ready(function () {
    // 方案 A 屬性
    var random61 = Math.floor(Math.random() * 3) + 1;
    var random63 = Math.floor(Math.random() * 4) + 1;
    var random65 = Math.floor(Math.random() * 4) + 1;
    var random67 = Math.floor(Math.random() * 5) + 1;
    var random69 = Math.floor(Math.random() * 3) + 1;
    var random71 = Math.floor(Math.random() * 3) + 1;
 
    // 方案 B 屬性(確保整組不同)
    do {
        random62 = Math.floor(Math.random() * 3) + 1;
        random64 = Math.floor(Math.random() * 4) + 1;
        random66 = Math.floor(Math.random() * 4) + 1;
        random68 = Math.floor(Math.random() * 5) + 1;
        random70 = Math.floor(Math.random() * 3) + 1;
        random72 = Math.floor(Math.random() * 3) + 1;
    } while (
        random61 === random62 &amp;&amp;
        random63 === random64 &amp;&amp;
        random65 === random66 &amp;&amp;
        random67 === random68 &amp;&amp;
        random69 === random70 &amp;&amp;
        random71 === random72
    );
   
       $('#question{QID}').hide();
 
       var thisQuestion = $('#question{QID}');
// Fill the array
       $('input[type=text]:eq(0)', thisQuestion).val(random61);
       $('input[type=text]:eq(1)', thisQuestion).val(random63);
       $('input[type=text]:eq(2)', thisQuestion).val(random65);
       $('input[type=text]:eq(3)', thisQuestion).val(random67);
       $('input[type=text]:eq(4)', thisQuestion).val(random69);
       $('input[type=text]:eq(5)', thisQuestion).val(random71);
       $('input[type=text]:eq(6)', thisQuestion).val(random62);
       $('input[type=text]:eq(7)', thisQuestion).val(random64);
       $('input[type=text]:eq(8)', thisQuestion).val(random66);
       $('input[type=text]:eq(9)', thisQuestion).val(random68);
       $('input[type=text]:eq(10)', thisQuestion).val(random70);
       $('input[type=text]:eq(11)', thisQuestion).val(random72);
});
</script>

You see the line $('#question{QID}').hide(); to hide the question.


By the way: Are you sure that you use the correct operator "AND"?
Shouldn't it be "OR"?

Furthermore you seem to use the old "green apple - banana" example.
But why didn't you use a question óf type array to merge the radio buttons.
 

Here is my proposal.
 

File Attachment:

File Name: limesurvey...9373.lss
File Size:86 KB


Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 2 days 21 hours ago by Joffm.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose