Skip to content

Instantly share code, notes, and snippets.

@pneff
Created February 14, 2014 08:33
Show Gist options
  • Select an option

  • Save pneff/8997666 to your computer and use it in GitHub Desktop.

Select an option

Save pneff/8997666 to your computer and use it in GitHub Desktop.
Anki Card Type for multiple-choice
<div class="check-container"><span id="check"></span></div>
{{#Question}}
<p>{{Question}}</p>
{{/Question}}
{{#Image}}
<p class="image">{{Image}}</p>
{{/Image}}
<ul>
{{Answers}}
</ul>
{{#Explanation}}
<p class="explanation">{{Explanation}}</p>
{{/Explanation}}
<script>
(function () {
var data = JSON.parse(window.title),
els = document.getElementsByTagName('input'),
count = els.length,
li_nodes = [],
ul,
idx,
correct = true;
for (idx = 0; idx < count; idx++) {
if (!!data.answered[idx] == !!els[idx].checked) {
els[idx].parentNode.className = 'success';
} else {
els[idx].parentNode.className = 'error';
correct = false;
}
}
if (correct) {
document.getElementById('check').innerHTML = 'Correct';
document.getElementById('check').className = 'success';
} else {
document.getElementById('check').innerHTML = 'Wrong';
document.getElementById('check').className = 'error';
}
// Re-order to same order as previous page
els = document.getElementsByTagName('li');
ul = els[0].parentNode;
for (idx = count - 1; idx >= 0; idx--) {
li_nodes.unshift(els[idx]);
ul.removeChild(els[idx]);
}
for (idx = 0; idx < count; idx++) {
ul.appendChild(li_nodes[data.position[idx]]);
}
}());
</script>
Question:
Which answer is the right one?
Choices:
<li><label><input type="checkbox" /> Answer 1</label></li>
<li><label><input type="checkbox" /> Answer 2</label></li>
<li><label><input type="checkbox" /> Answer 3</label></li>
Answers:
<li><input disabled type="checkbox" checked /> Answer 1</li>
<li><input disabled type="checkbox" /> Answer 2</li>
<li><input disabled type="checkbox" /> Answer 3</li>
{{#Question}}
<p>{{Question}}</p>
{{/Question}}
{{#Image}}
<p class="image">{{Image}}</p>
{{/Image}}
<ul>
{{Choices}}
</ul>
<script>
// window.title is persisted between question and answer, so we use it for our little hack
(function () {
var data = {answered: {}, position: []};
function onClick (ev) {
ev.stopPropagation();
data.answered[ev.currentTarget.getAttribute('idx')] = ev.currentTarget.checked;
window.title = JSON.stringify(data);
}
var els = document.getElementsByTagName('input'),
li_nodes = [],
count = els.length,
ul,
pos,
idx,
new_order = [];
for (idx = 0; idx < count; idx++) {
els[idx].setAttribute('idx', idx);
els[idx].addEventListener('click', onClick);
pos = Math.floor(Math.random() * count);
data.position.splice(pos, 0, idx);
}
// Re-order randomly
els = document.getElementsByTagName('li');
ul = els[0].parentNode;
for (idx = count - 1; idx >= 0; idx--) {
li_nodes.unshift(els[idx]);
ul.removeChild(els[idx]);
}
for (idx = 0; idx < count; idx++) {
ul.appendChild(li_nodes[data.position[idx]]);
}
window.title = JSON.stringify(data);
}());
</script>
* {
box-sizing: border-box;
}
html {
height: 100%;
}
.card {
margin: 0;
padding: 0;
padding: 20px;
height: 100%;
font-family: arial;
font-size: 20px;
text-align: center;
color: black;
}
.hint {
margin-top: 20px;
font-size: 70%;
color: #aaa;
}
.image {
height: 50%;
}
li {
text-align: left;
list-style: none;
margin: 10px;
}
li input {
margin-left: -20px;
}
ul br { display: none; }
.error {
background-color: #f2dede;
}
.success {
background-color: #dff0d8;
}
.explanation {
font-size: 14px;
text-align: left;
border-top: 1px solid #ccc;
padding-top: 10px;
margin-bottom: 10px;
}
.check-container {
position: fixed;
left: 0;
right: 0;
top: 0;
}
#check {
padding: 2px 10px;
}
@bmalnersic

Copy link
Copy Markdown

Hi,

first of all BIG THANKS for putting the code for "Anki Card Type for multiple-choice" on the Internet. Thanks for sharing. Card Type works beautifully on Windows Anki, a bit less in AnkiDroid (probably because there is no window.title property?). I wondered (before staring too take a hard look at the thing - I am not a programmer, but do fiddle with things a bit) if you would be so kind and maybe help me with the following:

  • provide Card Type for a single correct answer - radio buttons,
  • provide Card Type for a ordered multiple-choice answers (multiple, have to be in proper place, maybe by entering number before the answer( 1,2,3,4...),
  • make these Card Types work in AnkiDroid.

The reason that I would like to have these Card Types are the exams I am preparing for (Microsoft). I usually read few books on subject, do a lot of work in labs I set up, and then prepare for exam using dumps. I copy these to Anki and prepare desks. Since Anki is such a super tool for learning by spaced repetition, this turns out to be great fun way.

Thanks again, have a really nice, pleasant day,

Blaz

@bmalnersic

Copy link
Copy Markdown

Seems there is no way to make this work right on AnkiDroid ;(... Found this post: https://groups.google.com/forum/#!topic/anki-android/Iqs-cZN5Lr4. In AnkiDriod WebView is re-created on each card to avoid memory-leak issue...
But how does close work then? Somehow it must be possible to transfer answers from front to back.

@Vectraat

Copy link
Copy Markdown

I'm slightly confused about the instructions.
I copy and pasted "Front Template" "Styling" and "Back Template" into Anki on my PC under "Card Types for Basic" but I don't know what to do with "Example Note" ? Some further instruction would be greatly appreciated!

@tmbirhead

Copy link
Copy Markdown

Could you give some more details:

  1. What fields do I need to make for the cards
  2. How do I specify which answer is correct

When I tried to paste this code into Anki, and added the fields that I thought I needed, it didn't work.

I guess, in summary, please add some explanation on how to make this work! Thanks!

  • I'm completely computer illiterate. I just started doing the Programming course on Khan academy... So, yeah!

@ankitest

Copy link
Copy Markdown

This code works bad way. I've copy-pasted it's chunks under Windows, but each time I see BackSide after incorrect answer it paints randomly answer's in wrong colors. 👎

@phanminhtan7211

phanminhtan7211 commented Jun 20, 2018

Copy link
Copy Markdown

Thank you very much. This code works very good on AnkiDroid for windows and it works bad on AnkiDroid for Android
mutichose

@kelciour

Copy link
Copy Markdown

Thank you for the card template! I was asked to modify it a bit and shared it on https://ankiweb.net/shared/info/492884834

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment