Code to Wrap Cloze Answers in Curly Braces

I have had this code generated and I am wondering why it is not working at all (putting the curly braces or square brackets around the cloze card. I have tried putting pseudoelements in the CSS but the problem is that it does not have any effect on the DOM so that the brackets are copyable.

.cloze::before
content: “[[”;

.cloze::after
content: “]]”; /* Hidden marker after the cloze */

I am using this since I would be plugging text from my cloze note and plug it into an AI to make a direct question on it. I would need the braces to mark the position of the cloze answer for it to work though.

So could someone please try to get the code below working :question:

<script>

// Wait for Anki's jQuery to load
(function() {
    let tryCount = 0;
    const tryInterval = setInterval(() => {
        if (typeof $ !== 'undefined') {
            clearInterval(tryInterval);
            setupClozeModification();
        } else if (++tryCount === 100) {
            clearInterval(tryInterval);
            console.error("Anki's jQuery not found. Cloze modification not applied.");
        }
    }, 100);
})();

function setupClozeModification() {
    $(document).on('review:showAnswer', (evt) => {
        const cloze = evt.card.container[0];
        if (cloze.classList.contains('back')) {
            const clozeSpans = cloze.querySelectorAll('span.cloze');

            clozeSpans.forEach(span => {
                const revealSpan = span.querySelector('.cloze-reveal');
                if (revealSpan) {  // Ensure cloze is revealed
                    revealSpan.textContent = `[${revealSpan.textContent}]`;
                }
            });
        }
    });
}

</script>

Maybe someone else knows more, but since dae posted this here not long ago: JQuery seems to be broken after V3 updates to website.

I guess JQuery won’t really work, unless you load it externally. dae also shows in the linked post, what you could do if you really have to rely onto jquery.
Maybe try vanilla JavaScript if this won’t work either?

1 Like

Hey thanks for the reply :slight_smile:

Hmm well i dont really know much about coding. I put your idea into the AI though and it concurred that jQueries are broken. Here is one without the jquery dependency. It did cause the card to buffer before loading which means something ought to have changed yet I see no wrapping.

// Wait until the document is fully loaded
document.addEventListener('DOMContentLoaded', () => {
   setupClozeModification();
});

function setupClozeModification() {
   // Add event listener for Anki's answer reveal
   document.addEventListener('review:showAnswer', (evt) => {
       const cloze = evt.detail.card.container;
       if (cloze.classList.contains('back')) {
           const clozeSpans = cloze.querySelectorAll('span.cloze-reveal');
           clozeSpans.forEach(span => {
               const text = span.textContent;
               if (!text.startsWith('[') && !text.endsWith(']')) {
                   // Create new text nodes with brackets
                   const openBracket = document.createTextNode('[');
                   const closeBracket = document.createTextNode(']');
                   span.insertBefore(openBracket, span.firstChild);
                   span.appendChild(closeBracket);
               }
           });
       }
   });
}

AI is good for really simple stuff and learning, but if it gets more complex it won’t help you as much as you think because of the errors it does.
The ‘DOMContentLoaded’ addEventListener will not work in Anki, because the DOM is only partially refreshed when reviewing a card.
REMEMBER: AI is still not that far, to code you a error free template. Eventually someday.

EDIT:
I now understood you question better:
You can do that, what you’re trying to do, on your own. But it still would afford a little JS.
Maybe try it with regex?

1 Like

I don’t even know if you really can change clozes in that way, where you can set [[ and ]] instead of {{ and }}.

No no it is not that I want to be changed. :smile:

I just want the cloze answer to be wrapped in something whatever it may be like markers whether it be square brackets or curly braces.

for example

Front Side

X is the […] of Y.

Back Side

X is the @catalyst@ of Y

@ could be anything from quotation marks, braces, brackets etc. etc.

I am just trying to replicate the effect of

.cloze::before
content: “[[”;

cloze::after
content: “]]”; /* Hidden marker after the cloze */

but making the braces able to be copied and not just be pseudoelements, you know, like real characters.

I now understood you question better:
You can do that, what you’re trying to do, on your own. But it still would afford a little JS.
Maybe try it with regex?

The problem is I don’t know how to code javascript on my own. I was wondering if I could get help. :disappointed:

JavaScript is sadly only something which you would have to learn, so that people could help you more precisely. Because when you have no understanding, you’re just asking people to code stuff completely for you.
In the end, even if I would code it for you, we wouldn’t even know if this would really be the vision you like. This could take hours.
The code the AI gave you, I can just barely understand it and a lot of it won’t work in Anki. It also thinks of clozes as a HTML-Element, which I don’t even know if it is the case in Anki.

If you still want to learn JavaScript, here is my idea on how you could achieve your vision:

Maybe try catching stuff with regex, append a HTML element to it including a class and then put it back into the innerHTML of the HTMLElement.
Try your luck with positive lookaheads and lookbehinds in regex. This will be your best bet for such a job. (I can’t give you a promise here, that this will work).

2 Likes

This is a lot more complicated than I thought it would be since I thought there could be an easy way to replicate how the CSS code does it to be real text characters. But thanks anyway for letting me know that I should not be wasting more time with AI because I have been trying nonstop with it. :face_with_diagonal_mouth:

1 Like

Yep. Tried again, and the AI gave me once more a code that failed to do something. And then went ahead again to use DOMContentLoaded so it is going in circles at this point. Is what I am asking even possible to begin with :question:

And if not, is there any CSS substitute that I can use to format .cloze which makes it copyable

Yeah. But you would have to do it on your own and not with an AI. Because the AI doesn’t know the specifics Anki has and how Anki works.

I know basic CSS but nothing about Javascript. Is there a formatting which is copyable that I could apply to .cloze. Like content after and content before (problem is that they are not copyable)

In my opinion you’re trying to go here the easy way copying something.
I won’t respond to this post anymore. You are clearly not trying to learn but rather trying to have a quick solution.

If you really want someone to code this for you then you would have to pay him.

2 Likes

I am not capable of putting time into learning JavaScript from the ground up since I am a medical student and my exams are in a month from now. So it is not that I am lazy or anything. Sorry, I didn’t make it clear at the start.

Thank you for your time. I will try to look out for more stuff.

If you want the .cloze style to be applied to the strings surrounding the .cloze element as well, try the following:

<script>
    document.querySelectorAll(".cloze").forEach((cloze) => {
        cloze.innerHTML = `[[${cloze.innerHTML}]]`;
    });
</script>


Or, if you do not want the .cloze style to be applied to the surrounding strings, try the following:

<script>
    document.querySelectorAll(".cloze").forEach((cloze) => {
        cloze.before("[[");
        cloze.after("]]");
    });
</script>

2 Likes

I have just got humbled by your answer. Damn I still have also a lot to learn.

I need to stop being such a hothead.

1 Like

THANK YOU SO SO MUCH. That is exactly what I need!!! I am very grateful to you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.