INTRO
Recodes can be either single choice or multiple choice. We need to add the necessary code to add a SCHEMA to the question page, for further filtering. That being said, let’s start with the cases
Case 1 - Recode based on first two digits (numbers)
This recode was created to sort through zip codes.
Given a zip code, entered by the respondent, some options will be selected based on first 2 numbers of the zip code, each option corresponding to a different region:
As with all recodes, we need to add the list of available options. Next, we will add the following code:
let previousAnswerCode = "Enter question code";
let previousAnswer = response.answers.find(answer => answer.questionCode == previousAnswerCode).value.toString();
let zipCod = previousAnswer.substring(0, 2);
let dictionary = {
/* answer codes selected in filter : [list of answer codes in this question to be selected]*/
1: [00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,30,31,32,45,46,47,48,49,53,54,55,56],
2: [00,01,02],
3: [00,01,02,03,04,05,06,07,08,09,10],
4: [11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,30,31,32,45,46,47,48,49,53,54,55,56],
5: [28,29,33,34,35,36,37,38,39,40,41,42,43,44,60,61,62,63,64,65,66],
6: [50,51,52,57,58,59,70,71,72,73,74,75,76,77,78,79,80,81,82,83,87,88,89],
7: [67,68,69,84,85,86,90,91,92,93,94,95,96,97,98,99],
8: [50,51,52,57,58,59,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99]
}
let matchingKeys = [];
for (let key in dictionary) {
// Check if the array associated with the key contains the first two digits of zipCod
if (dictionary[key].includes(parseInt(zipCod))) {
matchingKeys.push(key);
}
}
matchingKeys.forEach(element => {
//console.log(element)
document.querySelector(`#p_M3_Postalcode .form-check > input[value='${element}']`).checked = true;
});
document.querySelector("body").classList.add("d-none");
document.querySelector("#p_next").click();
Here is the breakdown:
- Modify the first line with the question code of the open text for the zip code
let previousAnswerCode = "Enter question code";
- This will find and save the first 2 numbers of the zipcode entered:
let previousAnswer = response.answers.find(answer => answer.questionCode == previousAnswerCode).value.toString();
let zipCod = previousAnswer.substring(0, 2);
- This next section is the one that needs to be modified. The answer code will be the number on the left side, and the list of numbers between brackets are all options that will select said option.
For example, if the entered zip code is "11235", the first 2 digits are "11". Therefore, options 1 and 3 will be selected, as 11 appears on the list of 1 and 3
let dictionary = {
/* answer codes selected in filter : [list of answer codes in this question to be selected]*/
1: [00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,30,31,32,45,46,47,48,49,53,54,55,56],
2: [00,01,02],
3: [00,01,02,03,04,05,06,07,08,09,10],
4: [11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,30,31,32,45,46,47,48,49,53,54,55,56],
5: [28,29,33,34,35,36,37,38,39,40,41,42,43,44,60,61,62,63,64,65,66],
6: [50,51,52,57,58,59,70,71,72,73,74,75,76,77,78,79,80,81,82,83,87,88,89],
7: [67,68,69,84,85,86,90,91,92,93,94,95,96,97,98,99],
8: [50,51,52,57,58,59,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99]
}
- The rest of the code will select the corresponding values, hide and skip the page
Case 2 - Recode to select options based on previous answer
This case is based of a survey where a list of brands is selected based on the country of the respondent.
Here is the code:
document.querySelector("body").classList.add("d-none");
const lang = document.querySelector('html').getAttribute('lang').substring(0, 2);
let checkboxes = document.querySelectorAll('input');
let langCode = 0;
// Click buttons depending on language
switch (lang) {
case "nb":
langCode = 1;
break;
case "sv":
langCode = 2;
break;
case "fi":
langCode = 3;
break;
default:
checkboxes.forEach(element => {
element.checked = true;
})
//Select all items
break;
}
let itemMapping = {
1: [8, 9, 10, 11, 12, 13, 14, 15, 16, 24], // COUNTRY 1
2: [0, 1, 2, 3, 4, 5, 6, 7, 22, 23, 24], // COUNTRY 2
3: [17, 18, 19, 20, 9, 5, 2, 21, 24] // COUNTRY 3
}
if (itemMapping[langCode]) {
itemMapping[langCode].forEach(element => {
checkboxes[element].checked = true;
});
}
document.querySelector("#p_next").click();
Let’s breakdown the code:
This will hide and skip the page. This has to be at the beginning and end of the code
document.querySelector("body").classList.add("d-none");
document.querySelector("#p_next").click();
We will get the country based on the survey language, for example, if the language is German, the country will be set as Germany
const lang = document.querySelector('html').getAttribute('lang').substring(0, 2);
let checkboxes = document.querySelectorAll('input');
let langCode = 0;
The Switch will assign a value to the variable langCode. It will work based on the language part of the survey link.
case "nb":
langCode = 1;
break;
Here you need to add the elements that need to be selected for each country, following the format
let itemMapping = {
1: [8, 9, 10, 11, 12, 13, 14, 15, 16, 24], // COUNTRY 1
2: [0, 1, 2, 3, 4, 5, 6, 7, 22, 23, 24], // COUNTRY 2
3: [17, 18, 19, 20, 9, 5, 2, 21, 24] // COUNTRY 3
}
This section doesn’t need to be touched. It will check all items based on the country selected before.
if (itemMapping[langCode]) {
itemMapping[langCode].forEach(element => {
checkboxes[element].checked = true;
});
}