JavaScript not working for Version ⁨24.11

I just installed the latest version of Anki, and want to integrate JavaScript code for a card template.
I tested with the following simple test code in Front Template for one of my cards:

<div>
    {{Front}}
    <p id="test">Text</p>
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            document.getElementById("test").innerText = "JavaScript is working";
        });
    </script>
</div>

and see that JavaScript is not working as it shows the content of {{Front}} and the text “Text”, but not “JavaScript is working” in Preview.

Is there anything to be done to start with JavaScript on Anki?
Sorry for such a basic question, but I couldn’t find a post asking the same.

Version ⁨24.11 (87ccd24e)⁩
Python 3.9.18 Qt 6.6.2 PyQt 6.6.1

Probably because of this (quote by @hkr):

You can’t use window.onload or window.addEventListener(‘DOMContentLoaded’, …), because Anki desktop only updates a part of the DOM tree when displaying a card rather than refreshing the whole page.

(to clarify: js does work if you use things that are supported. I have custom card templates that use a lot of js and it works fine, same for a bunch of other users here)

5 Likes

That was exactly it. Thanks!