# Losing variable when entering {{#each}} loop

**URL:** <https://forums.losant.com/t/losing-variable-when-entering-each-loop/3645>\
**Category:** Help\
**Tags:** experience\
**Created:** [March 5, 2021, 1:08am UTC](https://forums.losant.com/t/losing-variable-when-entering-each-loop/3645 "2021-03-05T01:08:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jules\_Huguenin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/jules_huguenin/32/4031_2.png) [@Jules\_Huguenin](https://forums.losant.com/u/Jules_Huguenin)\
**Post date:** [March 5, 2021, 1:08am UTC](https://forums.losant.com/t/losing-variable-when-entering-each-loop/3645/1 "2021-03-05T01:08:55Z")

</div>

Hi there,  
I am building a select input in a form. Each option of the select input comes from an array `pageData.assetTypes`, and I have a default value stored as a string in `pageData.type`.

If my default value exists, I want it to be selected by default in my select input. I have done the following:

> ```
> {{#if pageData.type}}
> {{#each pageData.assetTypes}}
> {{#eq pageData.type this}}
> <option value="{{this}}" selected>{{this}}</option>
> {{else}}
> <option value="{{this}}">{{this}}</option>
> {{/eq}}
> {/each}}
> {{else}}
> <option value="" selected disabled hidden>Choose here</option>
> {{#each pageData.assetTypes}}
> <option value="{{this}}">{{this}}</option>
> {{/each}}
> {{/if}}
> 
> ```

I’m loosing the `pageData.type` value when entering the `{{#each}}` loop. Am I doing something wrong?  
Cheers  
Jules

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [March 5, 2021, 1:14am UTC](https://forums.losant.com/t/losing-variable-when-entering-each-loop/3645/3 "2021-03-05T01:14:41Z")

</div>

When you’re inside an `{{#each}}` the context is entirely replaced with the object for the current iteration. If you want to access the root context, you can use `@root`. So you’ll need to use:

```nohighlight
{{#eq @root.pageData.type}}

```

`@root` and other special variables are described here:

[https://handlebarsjs.com/api-reference/data-variables.html#root](https://handlebarsjs.com/api-reference/data-variables.html#root)

We also have an excellent Deeper Dive Webinar on Advanced Templating that covers this concept and many others here:

> **[Deeper Dive Webinar Replay Advanced Templating](https://www.losant.com/deeper-dive-webinar-replay-advanced-templating)**
>
> Watch the Advanced Templating Deeper Dive Replay and learn how to increase your development efficiency in Losant through templating.

---

<div class="post-metadata">

**Author:** ![Jules\_Huguenin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/jules_huguenin/32/4031_2.png) [@Jules\_Huguenin](https://forums.losant.com/u/Jules_Huguenin)\
**Post date:** [March 5, 2021, 1:33am UTC](https://forums.losant.com/t/losing-variable-when-entering-each-loop/3645/4 "2021-03-05T01:33:23Z")

</div>

Thanks @Brandon_Cannaday, I knew I should have dig deeper into the documentation.  
It works fine now.  
I’ll put this webinar on my to watch later list.  
Cheers  
Jules
