0 votes
in SoSci Survey (English) by s168567 (165 points)

Thank you! The code works great and is very concise.
I have another question.. When I combine the random generator with dynamic element, for some reason it only works partially.
Here is the link to the page https://www.jdm.uni-mannheim.de/sosci/loanforecaster/?act=RCp1sYCDz9CIwSyyKakvAClR
When the RG01 is 1, it works as intended: the additional explanation ("questionN") only appears when the option is clicked.
However, when RG01=2, the additional explanation ("questionB") appears automatically and clicking the option doesn't work.
Below is my code for the dynamic element:

<script type="text/javascript">
<!--
var optionA = document.getElementById("SE01_01a"); 
var questionN = document.getElementById("EN01_qst"); 
var questionB = document.getElementById("EB01_qst"); 
 
function toogle() {
  if (optionA.checked) {
    questionN.style.display = "";
    questionB.style.display = "";
}  else {
    questionN.style.display = "none";
    questionB.style.display = "none";
}
}
SoSciTools.attachEvent(optionA, "click", toogle);

toogle();
// -->
</script>

Here is the PHP code on the page before the JS code:

if (value('RG01') == 1) {
  html('<p><img src="Decision01-unbias.png"></p>');
  question('EN01');
} else {
  html('<p><img src="Decision01-bias.png"></p>');
  question('EB01');
}

1 Answer

0 votes
by SoSci Survey (328k points)
selected by s168567
 
Best answer

Depending on the random generator, you have either the question EN01 on the page or the question EB01.

That means that this JavaScript will throw an error:

questionN.style.display = "none";
questionB.style.display = "none";

The difference is when the error is thrown. In condition 1, the first line works, and the script will stop working in the second line. In condition 2, the first line will stop the script.

There are different possible solutions. One is to package the question into a <div> and display/hide only that.

Another one is to test which element you have available:

if (questionN) {
    questionN.style.display = "none";
}
if (questionB) {
    questionB.style.display = "none";
}

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

...