# (Solved) Specific answer boxes for each cloze region (type-in answer)

**URL:** <https://forums.ankiweb.net/t/solved-specific-answer-boxes-for-each-cloze-region-type-in-answer/29310>\
**Category:** Card Design\
**Created:** [April 12, 2023, 3:15pm UTC](https://forums.ankiweb.net/t/solved-specific-answer-boxes-for-each-cloze-region-type-in-answer/29310 "2023-04-12T15:15:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![akni](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@akni](https://forums.ankiweb.net/u/akni)\
**Post date:** [April 12, 2023, 3:15pm UTC](https://forums.ankiweb.net/t/solved-specific-answer-boxes-for-each-cloze-region-type-in-answer/29310/1 "2023-04-12T15:15:33Z")

</div>

Perhaps you have seen many exercises like this, and there seems to be no feasible way to create flashcards other than using the type-in answer format. The default type-in answer format of Anki has a weakness in that when the answers are written as a long text string, users find it difficult to remember which answers have been entered and it is very difficult to edit them (especially for exercises that contain about 50 - 60 words on various tenses).

 ![image](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/8/88ac3f4fdcc4c7bddf28d63a78c9062df78b7f43.png)

I have created a draft with the function of replacing cloze regions with input fields, and the data entered will be passed to the original input fields. At this point, the original input fields will serve as a reference for users to know which answers they have entered.

The front:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/9/968906f1463a913bea6709eef567690f29aafb12.png)

The back:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/2/2a26af48ca894cc841a00bc6639c08e6c4c7bb90.png)

It seems to be working well, just missing a few functions such as decoration, improving answers, adjust the size of type boxes and adding spacing between answers, right?I haven’t completed those functions yet because there is still a bigger issue. The issue seems to be that your code only works on 1st card, and when you switch to 2nd card (whilel reviewing), **the Javascript code disappears**. Interestingly, this issue only occurs on Windows, while it works normally on AnkiDroid.

 ![Picture1](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/c/c4b4d28cf67892357e19e63477b8709362e44c51.jpeg)

I truly do not know what caused this error, but I think that if it can be fixed, this template will be a new way to build decks with complex answers (or if someone has already made this template, please let me know where I went wrong). Please let me know your thoughts!

-------------------------------------- Link and codes ----------------------------------------

My two notes for example: [type in answer.apkg - Google Drive](https://drive.google.com/file/d/1HqznFj8WoINPkWT1pZ0xK7_Znet_04uH/view?usp=sharing)

**Front card:**

```auto
{{cloze:Question}}
{{type:cloze:Question}}

<script>
const clozeElements = document.getElementsByClassName("cloze");
for (let i = clozeElements.length - 1; i >= 0; i--) {
  const cloze = clozeElements[i];
  const ordinal = cloze.dataset.ordinal;
  const input = document.createElement("input");
  input.type = "text";
  input.classList.add("cloze-input");
  input.dataset.ordinal = ordinal;
  cloze.parentNode.insertBefore(input, cloze);
  cloze.parentNode.removeChild(cloze);
}

const clozeInputs = document.getElementsByClassName("cloze-input");
const mainInput = document.getElementById("typeans");

for (let i = 0; i < clozeInputs.length; i++) {
  const clozeInput = clozeInputs[i];
  clozeInput.addEventListener("input", function(event) {
    let values = [];
    for (let j = 0; j < clozeInputs.length; j++) {
      values.push(clozeInputs[j].value);
    }
    mainInput.value = values.join(" ");
  });
}
</script>

```

**Back card**

```auto
{{type:cloze:Question}}<br>

{{Note}}

```

---

<div class="post-metadata">

**Author:** ![kleinerpirat](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/kleinerpirat/32/19863_2.png) [@kleinerpirat](https://forums.ankiweb.net/u/kleinerpirat)\
**Post date:** [April 12, 2023, 3:50pm UTC](https://forums.ankiweb.net/t/solved-specific-answer-boxes-for-each-cloze-region-type-in-answer/29310/2 "2023-04-12T15:50:10Z")

</div>

> [@akni](#):
>
> code only works on 1st card, and when you switch to 2nd card (whilel reviewing), **the Javascript code disappears**.

It is unlikely that the JS disappears, but it probably throws a syntax error:

> [@How to put a random field inside a div with a script?](https://forums.ankiweb.net/t/how-to-put-a-random-field-inside-a-div-with-a-script/21508/9):
>
> The issue is the const keyword. Because the document persists between cards, using const outside of functions (outermost scope) causes a SyntaxError. Use the keyword var instead or enclose the script in an IIFE, like: (() =\> { const fields = document.querySelectorAll(".sentence"); fields[3].hidden = false; })();

---

<div class="post-metadata">

**Author:** ![akni](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@akni](https://forums.ankiweb.net/u/akni)\
**Post date:** [April 12, 2023, 3:59pm UTC](https://forums.ankiweb.net/t/solved-specific-answer-boxes-for-each-cloze-region-type-in-answer/29310/3 "2023-04-12T15:59:08Z")

</div>

The javascript code definitely didn’t disappear, I just don’t know which word to use to describe that it’s not working because I’m not very familiar with the code. But I will try again with your method, thank you very much.  
p/s: But do you know why this error only occurs on the PC version and not on mobile? Many of my other decks that use JavaScript to play videos, extract text, or play audio also encounter similar issues

---

<div class="post-metadata">

**Author:** ![kleinerpirat](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/kleinerpirat/32/19863_2.png) [@kleinerpirat](https://forums.ankiweb.net/u/kleinerpirat)\
**Post date:** [April 12, 2023, 5:25pm UTC](https://forums.ankiweb.net/t/solved-specific-answer-boxes-for-each-cloze-region-type-in-answer/29310/4 "2023-04-12T17:25:27Z")

</div>

It’s because AnkiDroid rebuilds the document on each card flip, whereas Anki Desktop persists the document.

`var` can be redeclared, but `const` or `let` cannot. That means as soon as you flip the card, there will be an error if a variable is already defined with one of these keywords.

Here’s a more detailed explanation (the statement about 2.1.41 is wrong, as Damien commented below):

> [@How to create a script that modifies the content of the fields?](https://forums.ankiweb.net/t/how-to-create-a-script-that-modifies-the-content-of-the-fields/19397/6):
>
> Now you might ask: “What is the issue with using block scoped variables at the top level of a \<script\> tag, i.e. why is const and let working inside functions, but not outside?” A variable with the same name must not be declared multiple times in the same scope using the let or const keywords. Consider this script: let num = 1 let num = 42 The second line will throw the following error: Uncaught SyntaxError: Identifier 'num' has already been declared. Say you used let/const at the top-lev…

---

<div class="post-metadata">

**Author:** ![akni](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@akni](https://forums.ankiweb.net/u/akni)\
**Post date:** [April 13, 2023, 4:29am UTC](https://forums.ankiweb.net/t/solved-specific-answer-boxes-for-each-cloze-region-type-in-answer/29310/5 "2023-04-13T04:29:57Z")

</div>

I apologize for the delay in responding, you know, the time zone difference.  
Thank you so much, today I learned something new. It’s really difficult for someone like me who doesn’t have a good understanding of code to find a thread discussing my issue, let alone a constructive comment like yours. The code snippet now works perfectly on both mobile and desktop devices (for now), for those who are interested, the code snippet is as follows:

**Front**

```auto
{{cloze:Question}}
{{type:cloze:Question}}

<script>
(() => {
  const clozeElements = document.getElementsByClassName("cloze");
  for (let i = clozeElements.length - 1; i >= 0; i--) {
    const cloze = clozeElements[i];
    const ordinal = cloze.dataset.ordinal;
    const input = document.createElement("input");
    input.type = "text";
    input.classList.add("cloze-input");
    input.dataset.ordinal = ordinal;
    cloze.parentNode.insertBefore(input, cloze);
    cloze.parentNode.removeChild(cloze);
  }

  const clozeInputs = document.getElementsByClassName("cloze-input");
  const mainInput = document.getElementById("typeans");

  for (let i = 0; i < clozeInputs.length; i++) {
    const clozeInput = clozeInputs[i];
    clozeInput.addEventListener("input", function(event) {
      let values = [];
      for (let j = 0; j < clozeInputs.length; j++) {
        values.push(clozeInputs[j].value);
      }
      mainInput.value = values.join(" ");
    });
  }
})();
</script>

```

**Back**

```auto
{{type:cloze:Question}}<br>

{{Note}}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [May 14, 2023, 6:44am UTC](https://forums.ankiweb.net/t/solved-specific-answer-boxes-for-each-cloze-region-type-in-answer/29310/7 "2023-05-14T06:44:58Z")

</div>

Closed on May 14, 2023, 6:44am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
