# Save HTML select tag selection for reload value in config screen

**URL:** <https://discourse.nodered.org/t/save-html-select-tag-selection-for-reload-value-in-config-screen/58731>\
**Category:** Developing Nodes\
**Created:** [21 February 2022 06:56 UTC](https://discourse.nodered.org/t/save-html-select-tag-selection-for-reload-value-in-config-screen/58731 "2022-02-21T06:56:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![NicoloToscani](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@NicoloToscani](https://discourse.nodered.org/u/NicoloToscani)\
**Post date:** [21 February 2022 06:56 UTC](https://discourse.nodered.org/t/save-html-select-tag-selection-for-reload-value-in-config-screen/58731/1 "2022-02-21T06:56:33Z")

</div>

Hi,  
in my node I need to store the value of the selected `select` tag to be able to set the value when I re-open config screen.

Now when I open config display after first configuration, the first value in the list is always displayed.

What would be the best solution to use ?

Thanks,

Nicolò

---

<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:** [21 February 2022 08:37 UTC](https://discourse.nodered.org/t/save-html-select-tag-selection-for-reload-value-in-config-screen/58731/2 "2022-02-21T08:37:48Z")

</div>

> [@NicoloToscani](#):
>
> in my node I need to store the value of the selected `select` tag to be able to set the value when I re-open config screen.

You store it as a property if the node... Just like you would any value you want to store as part of the node's configuration.

1. Add a property to your nodes `defaults` object
2. Give you select the appropriate `id` value and the editor will automatically map the node property to the value of the select.

[https://nodered.org/docs/creating-nodes/config-nodes#defining-a-config-node](https://nodered.org/docs/creating-nodes/config-nodes#defining-a-config-node)

Its a bit more complicated if you are dynamically generating the contents of the select box.

---

<div class="post-metadata">

**Author:** ![NicoloToscani](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@NicoloToscani](https://discourse.nodered.org/u/NicoloToscani)\
**Post date:** [21 February 2022 08:51 UTC](https://discourse.nodered.org/t/save-html-select-tag-selection-for-reload-value-in-config-screen/58731/3 "2022-02-21T08:51:50Z")

</div>

Thanks,

I'm generating dynamically the contents of the select tag.  
A different box with different ID is enabled based on the value selected in a previous box.

 ![device-config](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/164caff167c29be6a44382088477f9b7929e1a7c.png)

 ![data-config](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/367b4831b9ce89cdc4169f30228cee01252676f6.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:** [21 February 2022 09:37 UTC](https://discourse.nodered.org/t/save-html-select-tag-selection-for-reload-value-in-config-screen/58731/4 "2022-02-21T09:37:19Z")

</div>

In which case you'll need to explicitly set the selected value in your `oneditprepare` function after you have populated it.

You will still need to add a property to the node to store the selected value.

---

<div class="post-metadata">

**Author:** ![NicoloToscani](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@NicoloToscani](https://discourse.nodered.org/u/NicoloToscani)\
**Post date:** [21 February 2022 10:46 UTC](https://discourse.nodered.org/t/save-html-select-tag-selection-for-reload-value-in-config-screen/58731/5 "2022-02-21T10:46:18Z")

</div>

This is what I tried (but it doesn't work).  
Once selected the correct value I set a variable `data` in `oneditsave`.

```
    oneditsave: function () {
    
         <!--Check wich device type is selected-->
         let deviceType = $("#node-input-device option:selected");
         
                <!--PAC2200-->
                if(deviceType.val() == "pac2200") {
                    
                    let dataValue = $("#node-input-data-pac2200 option:selected");
                    this.data = dataValue.val();
          
                } 

```

In `data` I have the value selected in the box. After this in `oneditprepare` I set previous selected box with the last value of `data` .

```
    oneditprepare: function() {
        
        $("#node-input-device").on("change",function() {
            if (this.value === "pac2200") {
                $("#node-input-data-pac2200").show();
                $("#node-input-data-pac1200").hide();
                $("#node-input-data-pac1600").hide();
                $("#node-input-data-pac3100").hide();
                $("#node-input-data-pac3120").hide();
                $("#node-input-data-pac3220").hide();
                $("#node-input-data-pac3200T").hide();
                $("#node-input-data-pac4200").hide();
                $("#node-input-data-pac5200").hide();
                $("#node-input-data-sem3").hide();
                $("#node-input-data-atc6300").hide();
                $("#node-input-data-3va").hide();
                $("#node-input-data-powercenter1000").hide();
                
                $('#node-input-data-pac2200').val(this.data);
                
            }

```

`data` is declared in `defaults` object

```auto
RED.nodes.registerType('measure',{
        category: 'sentron',
        color: '#009999',
        defaults: {
            name: {value:""},
            unitId: {value: "1",required:true},
            device: {value: "pac1200",required:true},
            data: {value: "", required: true}
        },
        inputs:1,
        outputs:1,
        icon: "serial.png",
        label: function() {
            return this.name||"measure";
        }

```

Thanks

---

<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:** [21 February 2022 11:16 UTC](https://discourse.nodered.org/t/save-html-select-tag-selection-for-reload-value-in-config-screen/58731/6 "2022-02-21T11:16:53Z")

</div>

> [@NicoloToscani](#):
>
> `$('#node-input-data-pac2200').val(this.data)`

At this point of your code `this` is not your node, so `this.data` is not defined.

At the start of your oneditprepare function, grab the value:

```auto
const myData = this.data

```

You can then use `myData` inside the change event handler.

---

<div class="post-metadata">

**Author:** ![NicoloToscani](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@NicoloToscani](https://discourse.nodered.org/u/NicoloToscani)\
**Post date:** [21 February 2022 13:58 UTC](https://discourse.nodered.org/t/save-html-select-tag-selection-for-reload-value-in-config-screen/58731/7 "2022-02-21T13:58:27Z")

</div>

Ok, now works!!!!!

Thanks.

---

<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:** [7 March 2022 13:58 UTC](https://discourse.nodered.org/t/save-html-select-tag-selection-for-reload-value-in-config-screen/58731/8 "2022-03-07T13:58:47Z")

</div>

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