Conditional type field

Hello! This is my first post here, sorry if I’m not doing things in a right way.

Problem Statement

Say I have a note type with three fields A, B and C. How can I create a card template with a typing box such that, depending on a condition on the C field, the typing box expects field A or field B?
My main problem is that Anki has a strong condition on the presence of only one type field in a card template, and it seems to be a syntactic condition. For instance, the following does not work:

<div id=field>{{type:A}}</div>
<div id=c hidden>{{C}}</div>

<script>
  var field = document.getElementById("field");
  var c = document.getElementById("c");

  if (cond(c)) {
    field.innerHTML = "{{type:B}}";
  }
</script>

I also tried some different approaches but in each case I have to write somewhere in my HTML {{type:A}} and {{type:B}}, which inevitably isn’t allowed by Anki.

Solutions ?

Some of the non-optimal solutions I thought about are the following:

  • Manually fill a D field in my note which will contain A or B depending on C (but I have a lot of cards and this will take a lot of time, furthermore I would have to do this for every new card I add to my deck)
  • Use the add-on allowing more than one type field per template, but this does not work on my version of Anki
  • Not care, never change the type field and answer B when I know that I should and not worry about Anki telling me I’m wrong (though this maybe defies the point of a type field)
  • Not care bis and remove the type field in the B case

If I had access to the code of type fields in Anki I think I would be able to slightly modify it in order to suit my needs, but I have been unable to find it anywhere. And maybe some of you have new ideas that I did not have? Or, maybe, you can confirm that what I want to do is impossible in vanilla anki.

Thanks anyway for reading.

Context

I’m using Anki to learn japanese vocabulary, and I use a type field in the english to japanese direction, in order to also teach myself kanjis.
However, some very frequently used japanese words are usually written without kanjis - only with kanas - though they have an official kanji spelling. I would then like Anki to expect the kana spelling when this is the mostly used one, and the kanjis otherwise. My problem is that I get my cards from Takoboto (which is very useful for a number of reasons: example sentences, multiple meanings of a word, quick card generation) and the generated cards have two main fields: Japanese (the kanjis) and Reading (the kanas), and even for words usually written in kanas, the Japanese field contains kanjis. Thus, I would like to switch between {{type:Japanese}} and {{type:Reading}} depending on the situation.

One could argue that the type field is unneeded when the japanese word is only written in kanas, since there is no kanji to learn, and that’s a fair point — maybe I’ll end up doing this if there’s no more practical solution. But I feel like typing my answer makes me more active in my recalling than imagining it in my head, so I would be happy keeping a type field for this case too.

Field D can contain anything, you just need to use conditional replacement.

1 Like

To clarify a little bit. There are two ways this can be done:

  1. Use a conditional replacement:

    <div id=field>{{^C}}{{type:A}}{{/C}}{{#C}}{{type:B}}{{/C}}</div>
    

    This will require typing A or B, based on whether field C is empty or not (respectively). But arbitrary conditions in the form of cond(c) are impossible.

  2. Use a custom typing HTML element and check the answer explicitly using JS, which will enable using any kind of condition (just as the first method, it can work on any platform).

    I have a template, which has this implemented here. You can use it as a reference for your code, or set up the template itself to work for your purpose (I have a version that I use myself in a way very similar way to what you’ve described). It can also be set up to accept both answers without manually setting any conditions – just type either A or B when reviewing a card, and it will be accepted as correct in both cases.

4 Likes

Thanks a lot for your detailed answer. I think I will try the first method, and write a python script to fill a new field automatically for all my cards.
Your code is great though, that’s impressive work, thanks for doing that for the community. But I don’t think my problem is worth spending a lot of time digging into your codebase to see what I can extract from it… Maybe I’ll try, at some point.

Thanks anyway!