====== Immediately Show Questions when a Certain Option is Selected ======
[[filters|Filters]] allow content or questions to be shown depending on previous answers -- however, sometimes an additional question should appear directly on the same questionnaire page. This is known as dynamic content because the display changes without a new HTML page being transmitted by the browser. Dynamic changes require [[:en:glossary#javascript|JavaScript]]: a programming language that runs in the participant's browser. The server does not undergo any of the changes at first; it only receives the responses after the participant clicks on "next".
Generally speaking, extensive modificitions within a questionnaire page are possible with JavaScript and the Document Object Model (DOM). However, sometimes this requires advanced programming knowledge. This manual is limited to how a question is shown or hidden depending on an answer.
**Tip:** If the JavaScript code doesn't work right away, please use the [[:en:general:browser-tools]] for troubleshooting.
===== The Basics =====
- In JavaScript there are //Event Handler//. These are functions that are triggered by the user's actions on the page. The actions generate an //Event//, and this triggers the associated JavaScript function, the //EventHandler//. Possible events are ...
* ''%%'click'%%'' -- the click on an element,
* ''%%'mouseover'%%'' -- moving the mouse over an element,
* ''%%'change'%%'' -- selecting an option in a dropdown or changing a text input field,
* ''%%'keypressed'%%'' -- a keystroke
* and many others.
- Each element of an (HTML) internet page has a whole range of properties. SoSci Survey creates the individual questionnaire pages as HTML pages -- and all questions and input fields are HTML elements, whose properties can be shaped by JavaScript. One of these properties is the display style (''style'') and the display mode (''display'') is a part of this. The display mode defines how an element is shown (''block'' or ''inline''), or that it simply should not be shown at all (''none'').
- The elements of an HTML page can be addressed most simply by referring to their HTML ID. SoSci assigns HTML IDs automatically for all input fields that are focused on the question/item ID.
* The simplest way to generate an element's ID is for you to use the [[:en:general:browser-tools]]. These tools can show you the HTML code of an element (e.g. a selection field) -- incl. the HTML ID (''%%id="AB01_01"%%''). Alternatively, you can display the source code of the HTML page in the browser and look for the corresponding element there.
* SoSci assigns an HTML ID to each question consisting of the question ID, an underscore, (''_'') and ''qst'' (e.g. ''AB01_qst'' for question "AB01").
- In SoSci Survey, any content can be incorporated into a questionnaire page. Therefore, this includes JavaScript code. The easiest way of doing this is to save the code as a text element in **Text Elements and Labels**(for the //display// you have to select "HTML-Code") and dragging and dropping this text element into the page in **Compose Questionnaire** and __into__ the respective question(s).
**Note:** The user has to be (invisible) on the page in order to insert questions dynamically. Do not insert/hide compulsory questions: even if the participant cannot answer hidden questions, SoSci Survey insists on a response for compulsory questions.
**Note:** The JavaScript code should be designed to hide unnecessary content; not show necessary content. This has the advantage that participants with JavaScript deactivated can still answer the question as the question was not hidden.
**Note:** Screenreaders, which enable people with visual impairments to take part in your survey, do not always cope with dynamic content. Using this feature can reduce the accessibility of your questionnaire.
**Note:** Some question types use a different display for each screen by default (responsive layout). In rare cases, the display is not selected correctly when you display such a question via JavaScript. In this case, please set another option than "dynamic" for //display// in the question.
**Note:** If you use a simple selection or scale (i.e. no multiple selection) as a filter question and you have activated the option ''Deselect checkbox in simple selection or scale'' in the questionnaire, the property ''checked'' will still return ''true'' instead of ''false'' after deselecting an option. In this case, use the event ''change'' instead of ''click''.
**Tip:** If the participant sees that one of the selection options involves extra work, then this could influence his responsiveness. Here, a classic [[filters|Filter]] asking the second question on the following page could make more sense.
===== Example 1: Visible Selection =====
Depending on a yes/maybe/no question ("JN01", visible selection), a text question ("TX01") should be shown.
The following JavaScript code is saved as a text element (//display//: "HTML-Code"), dragged and dropped __below__ questions "JN01" and "TX01" in the questionnaire page.
===== Example 2: Drop-down =====
An additional question ("DD02"") should be shown depending on whether a certain option (e.g. those with the answer code "7") was selected in a drop-down selection ("DD01").
===== Example 3: Alternative Questions =====
You have a drop-down selection "AB05" and want to offer a further selection depending on the selection. In this example, another selection (place of interest/sight) must be created next to the first selection (favourite city) for every city and included on the page in order to do this.
{{:de:create:scr.dynamic.example2.de.png?nolink|Result in Questionnaire}}
**Tip:** If you also take a visible selection into consideration, [[:en:create:questions:extselect|Extended Selections]] facilitate a hierarchical selection of options in a dynamic version already.
**Tip:** The dynamic display of questions also works in combination with [[:en:create:table-layout|Placing Questions Side by Side]].
==== Questionnaire Page ====
Perhaps you cannot work with the standard HTML IDs available -- because you integrate a question multiple times on the page for some of them and display different items for each one. In this instance, the questions to be shown are enclosed with ''DIV'' elements. The questionnaire page will be rather long as result of this. For the first question, 4 pixels is set as the //Spacing to the next question// ({{:button.settings.png?nolink|Display Settings in the Questionnaire}}).
{{:de:create:scr.dynamic.page2.png?nolink|Questionnaire Page with All Necessary Elements}}
==== JavaScript Code ====
The following HTML/JavaScript code is saved in a text element (e.g. with the //ID// "js_dynamic", //display//: "HTML-Code"):
===== Beispiel 4: Skala =====
In this example, a scale item with 5 options (1="not at all" to 5="very often") is to be linked to text fields. The text field for each item should only appear if the option 3, 4 or 5 has been selected.
Furthermore, the solution shall not only be used for a pair of items, but for a whole battery (10 scale items and 10 text input fields, which are represented as [[:en:create:combine]]). To make this work without having to repeat the whole code 10 times, the JavaScript functionality is encapsulated as a class (''function''). This class is then called exactly once for each of the 10 items.
In the following example, the items have the following identifiers:
* Scales: SK01_01 to SK01_10
* Text inputs: TE01_01 to TE01_10
The JavaScript code for the 10 item pairs would look like this. A large part of the code is spent on first finding the selection fields of each scale item (at the top). This has the advantage that the code works independent of the scale level and of DK options.
The core of the function, which shows and hides the input field, is again the ''%%element.style.display = "none";%%''