The onclick event in HTML will change the original content

Anki 2.1.60 (76d88073) Python 3.9.15 Qt 5.15.2 PyQt 5.15.5
Platform: Windows-10-10.0.19041

For example, whether adding a new card or editing on an existing card, enter HTML in the editing window:<button onclick="this. innerHTML='textA'">textB</button>
A button will be generated.
Click the button in the editing window and the original code is changed to:<button onclick="this. innerHTML='textA'">textA</button>

That is, the behavior of the code will change my original content.
It shouldn’t be.
This will affect the realization of the function I want.

This is the expected behavior if you consider that the fields are contenteditable.

Maybe we can provide suggestions If you share more details about what you’re trying to do.

3 Likes

You may not understand what I mean.
This is like writing a program, and the normal operation of the user will change the code of the program itself.
Original code:


then:

After saving the card, the changed code will be saved

You’re not supposed to use inline logic in your field content. There are several good reasons why the editor works the way it does (e.g. to provide dynamic features such as the MathJax editor, image handle and most likely image occlusion in a future version).

HTML and JS should be separated as much as possible. You can easily prevent this issue by giving the button a class or id and selecting it from a script in your card template.

3 Likes

Oh, so this isn’t a bug?

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