It should be placed and triggered only on the front (or back) side of a card. But the problem is that it can be triggered on both sides of my cards. How to prevent this behavior?
I need to create two functions (one on the front and one on the back) that can be triggered by the same shortcut.
@hkr , Thanks for your reply.
When I’m trying to use your code a strange thing happens. The function I’m trying to invoke with the key shorcut starts to malfunction.
There is a variable that should be incremented by 1 every time the function is being triggered. For some reason when I’m using the key shortcut to do the job the variable is being incremented not correctly. It’s being incremented by some number (can be different each time).
What could cause this kind of a problem?
FRONT
<br>
<hr><br>
<div id="word">
Word: {{Word}}
</div>
<br><br>
<div id="definition">
Definition: {{Definition}}
</div>
<br><br>
<button onclick="showNextExample()">Show next example 🔄</button>
<br><br>
<div> Example: <br><br>
{{#Example1}}<div class="example">1: {{Example1}}</div>{{/Example1}}
{{#Example2}}<div class="example">2: {{Example2}}</div>{{/Example2}}
{{#Example3}}<div class="example">3: {{Example3}}</div>{{/Example3}}
{{#Example4}}<div class="example">4: {{Example4}}</div>{{/Example4}}
{{#Example5}}<div class="example">5: {{Example5}}</div>{{/Example5}}
</div>
<br><br>
<div>
Test: <div id="test"></div>
</div>
<script>
{
const examples = document.querySelectorAll(".example");
examples.forEach((element) => (element.style.display = "none"));
var number = 0;
examples[number].style.display = "block";
var testNumber = 1;
function showNextExample() {
if (number >= examples.length - 1) {
number = 0;
testNumber++;
}
else {
number++;
testNumber++;
}
examples.forEach((element) => (element.style.display = "none"));
examples[number].style.display = "block";
document.getElementById("test").textContent = testNumber;
}
function frontListener(event) {
if (event.key.toLowerCase() === "l") {
showNextExample();
}
}
if ("backListener" in globalThis) {
document.removeEventListener("keydown", backListener);
}
document.addEventListener("keydown", frontListener);
//display how the testNumber is being incremented
document.getElementById("test").textContent = testNumber;
}
</script>
testNumber variable is used here to show how the incrementation works when invoking the function either by a regular virtual button or with a key shortcut.
When studying the deck the variable increments like this:
First card – the variable inside the card is being incremended by 1 when pressing a key shortcut (as supposed to be)
Second card – the variable inside the card is being incremended by 2 when pressing a key shortcut
Third card – the variable inside the card is being incremended by 3 when pressing a key shortcut
etc.
Now it works most of the time fine but there is still a small issue when I make some adjustments to any card while learning/reviewing cards.
If I press “Edit” and make some changes to a card the function starts to malfunction and increments the variable by 2 instead of 1.
Or, for instance, if I go to “Tools –> Manage Note Types” while learning cards and make some changes to any of the note types and then go back to learn cards it also malfunctions.
So each time after editing a card or adjusting settings I need to exit the deck and enter it one more time. That’s very strange.