Trigger "show answer" in javascript

Is there a way to trigger the “show answer” action with javascript, from within the card that is being shown, just as clicking on the button would?
The problem is that I am trying to memorize long sentences, so I break them up and create a card for each piece of sentence, with the beginning of the sentence as context, and you have to type in the piece of the sentence. Unfortunately, it’s cumbersome to remember when to stop when typing a piece of the sentence, that is, for a sentence “I don’t like them, / Sam-I-am, / I don’t like them / here or there.” that is split up at the slashes, if I were prompted “I don’t like them, Sam-I-am, [type-in]”, and I typed “I don’t like them here or there”, I have typed too much.
I believe this impacts negatively the memorization, because learning by heart the exact same pieces each time is much more efficient (for me at least). However, this does not mean I remember when do these pieces end, and it would be counter-productive to remember that. I currently added a visual indicator that shows when I have exceeded the number of words required in the current piece, but I would like to try a different approach, that is, the input is submitted as-is as soon as the user has typed the right number of words (I think this would work well for me because I usually don’t make too many typos, so I don’t need to go back and correct a word once I have typed my input).

IIRC, you can do this in Ankidroid, but not in AnkiMobile or Desktop Anki.

Some kind of auto-reveal would probably make sense for type-the-answer questions. Even though I couldn’t what the exact behavior should be.

@ruin1990 uses the following function to auto-flip in his ClozeAdv template:

// flipToBack reference to https://github.com/git9527/anki-awesome-select
function flipToBack() {
	if (typeof pycmd !== "undefined") {
		pycmd("ans")
	} else if (typeof study !== "undefined") {
		study.drawAnswer()
	} else if (typeof AnkiDroidJS !== "undefined") {
		showAnswer()
	} else if (window.anki && window.sendMessage2) {
		window.sendMessage2("ankitap", "midCenter")
	}
}

You could use an input EventListener on your type-in-the-answer element and call abovementioned function when your criteria are met.

4 Likes

Anyone using this code should be aware that when using it on ios app, you need to first configure mid center tap to show answer in the preferences.

Also, what does the study.drawAnswer() do?

Good question, haven’t checked :smile:

Since my target platform is Desktop only, I stick to pycmd("ans"), which does exactly what I asked for.

Yes, this is already what I do :slight_smile:

Please bear in mind that these calls are not part of a public API, and they may break the next time this part of the code is refactored, so any shared decks/notetypes you distribute should be prepared for the symbols to be missing or to not respond.