# Conditional formatting (with JavaScript) doesn't work anymore

**URL:** <https://forums.ankiweb.net/t/conditional-formatting-with-javascript-doesnt-work-anymore/16797>\
**Category:** Help\
**Created:** [January 18, 2022, 5:30pm UTC](https://forums.ankiweb.net/t/conditional-formatting-with-javascript-doesnt-work-anymore/16797 "2022-01-18T17:30:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![smizzel](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@smizzel](https://forums.ankiweb.net/u/smizzel)\
**Post date:** [January 18, 2022, 5:30pm UTC](https://forums.ankiweb.net/t/conditional-formatting-with-javascript-doesnt-work-anymore/16797/1 "2022-01-18T17:30:25Z")

</div>

I have a note type that shows a quote that you have to judge is true or false.  
Based on the answer, the quote is highlighted in green or pink (light red).

The back template looks like this:

```auto
<div class="quote">"{{quote}}"<br></div>

<script>
let quoteanswer = document.querySelector(".quote");
switch("{{true or false}}") {
case 'true':
quoteanswer.style.backgroundColor = "lightgreen";
break;
case 'false':
quoteanswer.style.backgroundColor = "pink";
break;
}
</script>

```

This note type used to work perfectly, but recently a bug has appeared.  
The first time in a given study session a note of this type appears, it still works correctly, however all subsequent times the quote is no longer highlighted in the respective color.

Somehow the script is only executed correctly once.

What could be the cause of this?

---

<div class="post-metadata">

**Author:** ![hengiesel](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/hengiesel/32/920_2.png) [@hengiesel](https://forums.ankiweb.net/u/hengiesel)\
**Post date:** [January 18, 2022, 6:41pm UTC](https://forums.ankiweb.net/t/conditional-formatting-with-javascript-doesnt-work-anymore/16797/2 "2022-01-18T18:41:57Z")

</div>

Try replacing `let` with `var`.

---

<div class="post-metadata">

**Author:** ![smizzel](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@smizzel](https://forums.ankiweb.net/u/smizzel)\
**Post date:** [January 18, 2022, 8:12pm UTC](https://forums.ankiweb.net/t/conditional-formatting-with-javascript-doesnt-work-anymore/16797/3 "2022-01-18T20:12:56Z")

</div>

That worked! Why did it work? 🧐

---

<div class="post-metadata">

**Author:** ![hengiesel](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/hengiesel/32/920_2.png) [@hengiesel](https://forums.ankiweb.net/u/hengiesel)\
**Post date:** [January 18, 2022, 8:45pm UTC](https://forums.ankiweb.net/t/conditional-formatting-with-javascript-doesnt-work-anymore/16797/4 "2022-01-18T20:45:05Z")

</div>

The `<script>` will be executed every time, you go to the next card, but in the same context.

If you use a `let` assignment with the same identifier (`quoteanswer` in this case) multiple times, JavaScript will complain, that "`quoteanswer` has already been defined".  
However, with `var`, you’re allowed to redefine the variable.

TLDR, because this is valid JS:

```auto
var a = 5;
var a = 10; // OK: a is now 10

```

but this is not:

```auto
let a = 5;
let a = 10; // ERROR: a has already been defined

```

---

<div class="post-metadata">

**Author:** ![smizzel](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@smizzel](https://forums.ankiweb.net/u/smizzel)\
**Post date:** [January 18, 2022, 9:04pm UTC](https://forums.ankiweb.net/t/conditional-formatting-with-javascript-doesnt-work-anymore/16797/5 "2022-01-18T21:04:18Z")

</div>

Understood. Thanks for the clarification!  
Is it possible that a recent Anki update has made it so that cards are now rendered in the same context?

---

<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:** [January 18, 2022, 9:45pm UTC](https://forums.ankiweb.net/t/conditional-formatting-with-javascript-doesnt-work-anymore/16797/6 "2022-01-18T21:45:25Z")

</div>

> [@smizzel](#):
>
> Is it possible that a recent Anki update has made it so that cards are now rendered in the same context?

Yes, it’s like that since version 2.1.41. Before, these script tags didn’t persist between cards. AnkiDroid still handles this the “old” way and refreshes every time, but that’s not very efficient and they’ll likely follow Anki Desktop’s example some day in that regard.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [May 1, 2023, 4:35am UTC](https://forums.ankiweb.net/t/conditional-formatting-with-javascript-doesnt-work-anymore/16797/7 "2023-05-01T04:35:46Z")

</div>


