# Assign a particular field of each card to a variable using JavaScript

**URL:** <https://forums.ankiweb.net/t/assign-a-particular-field-of-each-card-to-a-variable-using-javascript/34339>\
**Category:** Help\
**Created:** [September 15, 2023, 9:06am UTC](https://forums.ankiweb.net/t/assign-a-particular-field-of-each-card-to-a-variable-using-javascript/34339 "2023-09-15T09:06:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sweetins](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@Sweetins](https://forums.ankiweb.net/u/Sweetins)\
**Post date:** [September 15, 2023, 9:06am UTC](https://forums.ankiweb.net/t/assign-a-particular-field-of-each-card-to-a-variable-using-javascript/34339/1 "2023-09-15T09:06:49Z")

</div>

From the back template of my cards, am trying to assign a field to a variable using;

```auto
<script>
let Variable={{field}};
</script>

```

However this happens only once at the start of my cards. It works only for the first card and that’s it, the variable maintains that value assigned to it in the first instance. Apparently the variable is not getting assigned at the start of each card.

Some suggestion were that I should have the variable assignment statement in a function, others were to use var, instead of let in my assignment but none of these seems to be working out. How do I work around this? Any suggestion will be much appreciated. Thanks already.

---

<div class="post-metadata">

**Author:** ![ankicat](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/ankicat/32/14749_2.png) [@ankicat](https://forums.ankiweb.net/u/ankicat)\
**Post date:** [September 15, 2023, 4:04pm UTC](https://forums.ankiweb.net/t/assign-a-particular-field-of-each-card-to-a-variable-using-javascript/34339/2 "2023-09-15T16:04:03Z")

</div>

I am a beginner.  
I don’t know the principle.  
I tried to use:

```auto
<div id="front">{{Field}}</div>
<div id="now"></div>
document.getElementById("now").innerHTML = document.getElementById("front").innerHTML
var virable = document.getElementById("front").innerHTML

```

It takes effect on multiple cards.  
Although let confuses me too and maybe var can avoid the problem, you can try the above method.  
😋

---

<div class="post-metadata">

**Author:** ![Eltaurus](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/eltaurus/32/10287_2.png) [@Eltaurus](https://forums.ankiweb.net/u/Eltaurus)\
**Post date:** [September 16, 2023, 4:35am UTC](https://forums.ankiweb.net/t/assign-a-particular-field-of-each-card-to-a-variable-using-javascript/34339/3 "2023-09-16T04:35:34Z")

</div>

Surround it with the backtick characters:

```auto
<script>
let Variable = `{{field}}`;
</script>

```

Anki simply replaces the double-curly-braced text with the contents of the respective fields, so think of it as such. If the field contains `someword`, the code passed to JS will be `let Variable = someword;`, so JS will interpret the right-hand side as a name for a variable, which is likely undeclared and will break things. But if it’s `let Variable = `someword`;` it will be treated as a string, so everything should be fine.

Of course, you have to make sure the {{field}} itself doesn’t contain any ``` inside, which is why ``` is probably a better choice for marking the contents of a string since `'` are often encountered in English shortenings such as `don't`, and `"` is likely to be present if the {{field}} has html formatting.

---

<div class="post-metadata">

**Author:** ![Sweetins](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@Sweetins](https://forums.ankiweb.net/u/Sweetins)\
**Post date:** [September 16, 2023, 7:47am UTC](https://forums.ankiweb.net/t/assign-a-particular-field-of-each-card-to-a-variable-using-javascript/34339/4 "2023-09-16T07:47:39Z")

</div>

Thanks for the response

---

<div class="post-metadata">

**Author:** ![Sweetins](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@Sweetins](https://forums.ankiweb.net/u/Sweetins)\
**Post date:** [September 16, 2023, 7:53am UTC](https://forums.ankiweb.net/t/assign-a-particular-field-of-each-card-to-a-variable-using-javascript/34339/5 "2023-09-16T07:53:26Z")

</div>

I actually had a bunch of other stuffs in that script tag ()… but upon isolating the assignments into their own tags, it seems to work just fine. Thanks for the response

---

<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:** [October 16, 2023, 7:54am UTC](https://forums.ankiweb.net/t/assign-a-particular-field-of-each-card-to-a-variable-using-javascript/34339/6 "2023-10-16T07:54:25Z")

</div>

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