0 votes
in SoSci Survey (English) by s057462 (330 points)
edited by SoSci Survey

Dear Dominik,

earlier we came to you for help regarding giving feedback to participants within an assignment question using JavaScript.

Now, we would like to ask for your help with the following:
we are trying to store the result of the feedback in an internal variable to make future analysis easier. We would like to have columns in the output file that state if the participant was wrong (0) or correct (1) on the item.

Could you give us more information on how to store the result of our feedback in an internal variable while using an assignment type question?

We tried to use the line below, found in the manual, to store the result in an internal variable but it did not work:

document.getElementById("AB01_01").value = "value A";

Bellow is how we tried to implement it in the feedback file.
We are trying to get the result of the function stored in a different item of the internal variable, so it doesn't override the previous one...
We did this with the %itemID% bit.
And then store a 0 in the internal variable if it's a wrong answer or a 1 if it's a correct answer.

Best wishes,

<div style="position: absolute; top: 27%; left: 50%; transform: translate(-50%, -50%); font-size:70px; color:#FF0000; font-weight:bold;" id="wrong">X</div>

<script type="text/javascript">
<!--

 function selFilter(item, option)
   {

      //indexOf returns -1 if the number is not found and something other than -1 if it is found

      //if the 'item' cannot be found in the variable IA01, so it is a trial where a right-hand 
       response is desired
      //and the response was left-handed (option1)

      //define itemID variable
      var itemID = item

      if ( (IA01.indexOf(item) == -1) && (option == 1))
         {
         // Show feedback
         showContent()         

         // Store result in variable. 0 = wrong
         console.log("%itemID%");
         document.getElementById("IV01_%itemID%").value = "0";


         // Ignore answer
         return -2;
          }

//otherwise continue with next trial
      else
          {

          // Store result in variable. 1 = correct
          console.log("%itemID%");
          // document.getElementById("IV01_%itemID%").value = "1";

          return 0;
          }
   }

SoSciTools.attachEvent(window, "load", function() {
    assignmentIA01.setCallbackSelect(selFilter);
});
 
// -->
</script>
by SoSci Survey (327k points)
First of all: Did you create internal variables in the "List of Quesions" and did you drag this "question" into the page (before the JavaScript code)? If not already read, please give this manual a look: https://www.soscisurvey.de/help/doku.php/en:create:questions:internal
by s057462 (330 points)
Yes we did make the internal variables in the list of questions and added items to it and dragged it into the questionnaire.

1 Answer

0 votes
by SoSci Survey (327k points)

Please note that placeholders (e.g., %itemID%) are replaced only once when the Code is sent to the browser, but not between the items in your question.

If I understand your question correctly, you'd like to store every trial in a different variable? Then try this one:

// This here at the beginning or your function
var itemID = String(item);
if (itemID.length < 2) itemID = "0" + itemID;
// This here where the placeholder is used
console.log("IV01_"+ itemID);
document.getElementById("IV01_"+ itemID).value = "0";

And as always, please keep an eye on the error console.

by s057462 (330 points)
So like below?

Here is a pretest link to the page:
https://www.soscisurvey.de/animals_and_people/?act=sZfdEWkGjcukZn1ewDgu3VdW



<div style="position: absolute; top: 21.5%; left: 50%; transform: translate(-50%, -50%); font-size:70px; color:#FF0000; font-weight:bold;" id="wrong">X</div>

<script type="text/javascript">
<!--
document.getElementById("wrong").style.display = "none";

  var IA01 = [1,2,3,4]

function showContent()
             {
             var content = document.getElementById("wrong");

             // restore the normal display mode
             content.style.display = "";
             /// start the timer
             window.setTimeout(function()
             {
             // Hide
             content.style.display = "none";
             }, 300); // 300ms
             }

   function selFilter(item, option)
   {
      var itemID = String(item);
      if (itemID.length < 2) itemID = "0" + itemID;

      if ( (IA01.indexOf(item) == -1) && (option == 1))
         {
         // Show feedback
         showContent()         

         // Store result in variable. 0 = wrong
         console.log("IV01_"+ itemID);
         document.getElementById("IV01_"+ itemID).value = "0";

else if ( (IA01.indexOf(item) != -1) && (option == 2))
          {
          // Show feedback and ignore answer
          showContent ()

          // Store result in variable. 0 = wrong
         console.log("IV01_"+ itemID);
         document.getElementById("IV01_"+ itemID).value = "0";

          return 0;
          }

      //otherwise continue with next trial
      else
          {

          // Store result in variable. 1 = correct
          console.log("IV01_"+ itemID);
          document.getElementById("IV01_"+ itemID).value = "1";
          return 0;
          }
   }
 
SoSciTools.attachEvent(window, "load", function() {
    assignmentIA01.setCallbackSelect(selFilter);
});
 
// -->
</script>
by SoSci Survey (327k points)
Yes, like that. Please note that, in your online code, there's still a %itemID% in the most recent ELSE part.
by s057462 (330 points)
So the code is writing 1s and 0s in the internal variable but they do not make sense...
They are not related to the feedback given...

If we answer all the trials wrong or correct, then the internal variable is correct.

But if we alternate between wrong and correct responses, the internal variable doesn't relate to the feedback...


E.g.
expected is 1 0 1 0 1 0 1 0
the internal variable gives 0 1 0 1 1 1 0 0

expected is 1 1 1 1 0 0 0 0
the internal variable gives 1 1 0 1 0 1 0 0
by SoSci Survey (327k points)
Could it be that you rotate your items randomly?

The code I have proposed will store the response per item - no matter where it appeared in an random order.
by s057462 (330 points)
I turned off the random rotation and as you predicted it works as expected now!
Perfect!
Thank you very much for the help.

Willkommen im Online-Support von SoSci Survey.

Hier bekommen Sie schnelle und fundierte Antworten von anderen Projektleitern und direkt von SoSci Survey.

→ Eine Frage stellen


Welcome to the SoSci Survey online support.

Simply ask a question to quickly get answers from other professionals, and directly from SoSci Survey.

→ Ask a Question

...