r/Anki•
Lil9

Cloze deletions: hide/show hints?

Hi everybody,

I've made a number of cloze deletion cards looking like this:

Je mange une pomme {{c1::rouge::red}}.

Is there a way to give me a "show hint" button at the front of my card, so that normally my card says

Je mange une pomme [..].

but when I click the hint button it says

Je mange une pomme [red].

?

Posted May 22, 2017, 8:43 PM UTC

25
ankitest•

Easy. By clicking each [..]

Also you can see the hint in title popup (on mouse over): rouge hints red

Next click will cloze [red] again into [..].

Just use JavaScript in your Front Template:

{{cloze:Text}}<br><br>

<script>
var clozes = document.getElementsByClassName('cloze'), filler = '[..]';
for (var i = 0; i < clozes.length; i++) {
  if (clozes[i].innerHTML != '[...]') {
	clozes[i].dataset.innerHtml = clozes[i].innerHTML; 
	clozes[i].innerHTML = filler;
	clozes[i].onclick = function(){
  		if (this.innerHTML != filler) {
			this.innerHTML = filler;
		} else {
			this.innerHTML = this.dataset.innerHtml;
		}
	}
	clozes[i].style.cursor = 'pointer';
	clozes[i].title = clozes[i].dataset.innerHtml;
  }
}
</script>

If you need exactly the button to hint all together now
then add one script more:

<script>
function hintAll() {
    for (var i = 0; i < clozes.length; i++) {
        if (clozes[i].innerHTML != '[...]') {
            (function(thiz) {
                if (thiz.innerHTML != filler) {
                    thiz.innerHTML = filler;
                } else {
                    thiz.innerHTML = thiz.dataset.innerHtml;
                }
            }(clozes[i]))
        }
    }
}
</script>

<input type="button" value="hint" title="Hint All Clozes" accesskey="Z" id="hintThemAll"  
          style="position:fixed;top:10px;left:20px;" onclick="hintAll();">

Hotkey Alt+Z does not work when focus is on Show Anser button.

You can disable this button, if Front Side does not have any cloze with hint at all:

<script>
var done;
done = 0;
for (var i = 0; i < clozes.length; i++) {
    if (clozes[i].innerHTML != '[...]') ++done;
}
if (done === 0) {
    document.getElementById('hintThemAll').setAttribute('disabled', 'disabled');
} else {
    document.getElementById('hintThemAll').disabled = false;
}
</script>

Please, pay attention: this script should go after INPUT button, not before.

If you want to create a placeholder for such button on the Back Side, you can use:

 <input type="button" value="hint"
    style="position:fixed;top:10px;left:20px;" disabled>

If you want to style such clozes other way than clozes without hint,
use in Styling section:

.cloze[data-inner-html] { color: red; }
5
Lil9OP•

Wow, that's great, thank you.

It works beautifully on PC and AnkiDroid with my existing massive-context cloze deletion cards.

I was contemplating to convert all my cards into standard cloze texts without hints and make an extra hint field like in this video, but your solution works even better for me. Thanks a lot!

2
ankitest•

Thanks for the interesting question.

1
Glutanimate•

Great job, once again! Your solutions never seize to impress.

2
ankitest•

Thanks. You can take a look at further modifications of these scripts
in the sample deck Anki Cloze OneByOne or on GitHub

2
[deleted]•

[deleted]

1