# How to create a script that modifies the content of the fields?

**URL:** <https://forums.ankiweb.net/t/how-to-create-a-script-that-modifies-the-content-of-the-fields/19397>\
**Category:** Card Design\
**Created:** [April 24, 2022, 10:31pm UTC](https://forums.ankiweb.net/t/how-to-create-a-script-that-modifies-the-content-of-the-fields/19397 "2022-04-24T22:31:16Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![kleinerpirat](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/kleinerpirat/32/19863_2.png) [@kleinerpirat](https://forums.ankiweb.net/u/kleinerpirat)\
**Post date:** [April 25, 2022, 3:30am UTC](https://forums.ankiweb.net/t/how-to-create-a-script-that-modifies-the-content-of-the-fields/19397/6 "2022-04-25T03:30:32Z")

</div>

> [@jcznk](#):
>
> Neither `let` or `const` seem to work with your script, while `var` seems to work fine.

> [@\[SOLVED\] HTML / CSS engine in Reviewer doesn't work like in Browser](https://forums.ankiweb.net/t/solved-html-css-engine-in-reviewer-doesnt-work-like-in-browser/16185/2):
>
> > Variables declared by `var` keyword are scoped to the immediate function body (hence the function scope) while `let` variables are scoped to the immediate _enclosing_ block denoted by `{ }` (hence the block scope).
> 
> From: [javascript - What’s the difference between using “let” and “var”? - Stack Overflow](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var)

Now you might ask: “What is the issue with using block scoped variables at the top level of a `<script>` tag, i.e. why is `const` and `let` working inside functions, but not outside?”

### A variable with the same name must not be declared multiple times in the same scope using the let or const keywords.

Consider this script:

```auto
let num = 1
let num = 42

```

The second line will throw the following error:

```auto
Uncaught SyntaxError: Identifier 'num' has already been declared.

```

Say you used let/const at the top-level of a `<script>` tag, which is the outermost scope. When Anki executes that script twice in the same context (i.e. when switching to a new card), it will throw that error.

This wasn’t an issue before Anki switched to _persisting its webview during review_. Before 2.1.41, the whole document was refreshed with each card, so you could use let/const/var interchangeably.

Now, you’ll need to use `var` at the outermost scope.

---

_[View the full topic](https://forums.ankiweb.net/t/how-to-create-a-script-that-modifies-the-content-of-the-fields/19397)._
