I’m using AndkiDroid 2.20.1 (anki 24.11).
I want to create a simple card template that shuffles the list items in a certain field, let’s say {{Choices}}.
The list I have in {{Choices}} has id="content"
, e.g.:
<div>[Choices]</div><br>
<ol id="content">
<li>Choice1 [Front]</li>
<li>Choice2 [Front]</li>
<li>Choice3 [Front]</li>
</ol>
And I expect the template below to get the list and show the items in a shuffled order.
<div>{{Front}}</div>
<div>
{{Choices}}
<script>
(function() {
function isFrontSide() {
return !document.getElementById("answer");
}
function shuffleList() {
const list = document.getElementById("content");
if (!list) return;
const items = list.getElementsByTagName("li");
const itemsArray = Array.from(items);
if (itemsArray.length < 2) return; // No need to shuffle
for (let i = itemsArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[itemsArray[i], itemsArray[j]] = [itemsArray[j], itemsArray[i]];
}
// Remove all existing list items
while (list.firstChild) {
list.removeChild(list.firstChild);
}
// Append shuffled items back to the list
itemsArray.forEach(item => list.appendChild(item));
}
function initShuffle() {
if (isFrontSide()) {
shuffleList();
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initShuffle);
} else {
initShuffle();
}
})();
</script>
</div>
This works without problems on Anki Desktop and AnkiWeb, however, not on my AnkiDroid. What it does:
- Notes only show the shuffled list (including the one without
id="content"
) and no other content; for example, “[Choices]” in the example on top is not displayed - It also shuffles other lists that are in other fields, and without
id="content"
What’s more, even on AnkiDroid, when I click on the note and view the sample (the ‘eye’ icon), it displays the note as expected without causing the issues above.
I’ve tested the following, and they work fine on my AnkiDroid. But they do a lot more than I need to and I would rather fix the template above also to avoid having to reorganize all the notes I have.
Any advice would be appreciated.