# Setting initial dropdown value for dynamic dropdown?

**URL:** <https://discourse.nodered.org/t/setting-initial-dropdown-value-for-dynamic-dropdown/29331>\
**Category:** Developing Nodes\
**Created:** [1 July 2020 18:37 UTC](https://discourse.nodered.org/t/setting-initial-dropdown-value-for-dynamic-dropdown/29331 "2020-07-01T18:37:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![neile](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/neile/32/25016_2.png) [@neile](https://discourse.nodered.org/u/neile)\
**Post date:** [1 July 2020 18:37 UTC](https://discourse.nodered.org/t/setting-initial-dropdown-value-for-dynamic-dropdown/29331/1 "2020-07-01T18:37:44Z")

</div>

I'm creating a custom node that reads a list of effect values values dynamically from a remote server and populates a dropdown for its edit page. I've got the dropdown populating just fine, and the resulting selected effect makes its way to my code at node execution time just fine as well.

Where I'm struggling is setting the value of the dropdown when the edit page opens to the last value selected by the user. The only reference I can find to how to do this is [an old message from 2017 on StackOverflow](https://stackoverflow.com/a/42854811). Unfortunately when I simply try and use _this.effect_ in code it's always _undefined_. Debugging a bit shows that _this_ is always the overall window object, not my node object.

Any tips on what I'm missing here? I'm sure it's something obvious. [The complete edit page HTML is on github](https://github.com/danecreekphotography/node-red-contrib-wled2/blob/main/src/wled2.html). Thanks!

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [1 July 2020 18:43 UTC](https://discourse.nodered.org/t/setting-initial-dropdown-value-for-dynamic-dropdown/29331/2 "2020-07-01T18:43:10Z")

</div>

You need to use jquery to set the dropdown to the value saved in your config. For example look at the code of one of my nodes where it is this line:

> <https://github.com/johanneskropf/node-red-contrib-sox-utils/blob/ef4f9e5d3afd9d4d9b1f91c987a98f9df2597665/sox_play.html#L43>

---

<div class="post-metadata">

**Author:** ![neile](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/neile/32/25016_2.png) [@neile](https://discourse.nodered.org/u/neile)\
**Post date:** [1 July 2020 18:50 UTC](https://discourse.nodered.org/t/setting-initial-dropdown-value-for-dynamic-dropdown/29331/3 "2020-07-01T18:50:26Z")

</div>

Agreed. My problem is with this in your example, which doesn't seem to work for me:

> <https://github.com/johanneskropf/node-red-contrib-sox-utils/blob/ef4f9e5d3afd9d4d9b1f91c987a98f9df2597665/sox_play.html#L37>

When I do that and dump it via _console.log()_ it's the entire Window object. Attempting to directly access the value with _node.effect_ returns _undefined_.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/e/3ecf862a1887bd247d231c160f974d47a2f218de.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 July 2020 19:03 UTC](https://discourse.nodered.org/t/setting-initial-dropdown-value-for-dynamic-dropdown/29331/4 "2020-07-01T19:03:02Z")

</div>

The problem is you have defined your oneditprepare as an arrow function: [https://github.com/danecreekphotography/node-red-contrib-wled2/blob/91401ae919c4c39f89cfd73aad3a7b55b2272ba0/src/wled2.html#L28](https://github.com/danecreekphotography/node-red-contrib-wled2/blob/91401ae919c4c39f89cfd73aad3a7b55b2272ba0/src/wled2.html#L28)  
You cannot do that here - it needs to be a regular function so that `this` is define.

---

<div class="post-metadata">

**Author:** ![neile](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/neile/32/25016_2.png) [@neile](https://discourse.nodered.org/u/neile)\
**Post date:** [1 July 2020 19:24 UTC](https://discourse.nodered.org/t/setting-initial-dropdown-value-for-dynamic-dropdown/29331/5 "2020-07-01T19:24:32Z")

</div>

Arrrrggggggh. I knew it was something simple. Thank you, works like a charm now!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 August 2020 19:26 UTC](https://discourse.nodered.org/t/setting-initial-dropdown-value-for-dynamic-dropdown/29331/6 "2020-08-30T19:26:12Z")

</div>

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