# Dynamic drop-down list issue in node-red editor for custom node

**URL:** <https://discourse.nodered.org/t/dynamic-drop-down-list-issue-in-node-red-editor-for-custom-node/81474>\
**Category:** Developing Nodes\
**Created:** [21 September 2023 14:25 UTC](https://discourse.nodered.org/t/dynamic-drop-down-list-issue-in-node-red-editor-for-custom-node/81474 "2023-09-21T14:25:47Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [27 September 2023 08:23 UTC](https://discourse.nodered.org/t/dynamic-drop-down-list-issue-in-node-red-editor-for-custom-node/81474/21 "2023-09-27T08:23:48Z")

</div>

NR offers implicit functionality for developers convenience - yet this functionality just covers the basic tasks for creating nodes. You're trying to go the convenience way - but linking two dynamic dropdowns is beyond those basic tasks.

Being explicit solves your issue within one line:

```javascript
        oneditprepare : function() {
            console.log("## on edit prepare");		
            
            let node = this; // <--- see comment below!

            console.log(node.meal_type, node.meal);
			
			$('#node-input-meal_type').on('change', function() {
			    console.log("## meal type is changed ##");			
				var sel = document.getElementById("node-input-meal_type");
				console.log("## selected meal type: " + sel.value);
				var drop = document.getElementById("node-input-meal");

				if(sel.value == 'breakfast') {
					var opt1 = document.createElement('option');
					opt1.value = "sel";
					opt1.innerHTML = "Select";
					opt1.selected = true;
					drop.appendChild(opt1);
                   
                    [...]
                   
                    drop.value = node.meal; // <--- be explicit !!

				} else if(sel.value == 'lunch') {}
				} else if(sel.value == 'dinner') {}
			});

```

Additionally, it's good practice to add - as I did - sth like

```auto
            let node = this;

```

to always have access to the nodes properties.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 September 2023 09:10 UTC](https://discourse.nodered.org/t/dynamic-drop-down-list-issue-in-node-red-editor-for-custom-node/81474/22 "2023-09-27T09:10:37Z")

</div>

> [@Pioneerj](#):
>
> Clean code is good practice.. But it is not helping to solve issue

Though I'm no longer a professional developer, 40+ years of experience has taught that, for me at least, clean code absolutely does help solve issues - including this one.

I rushed the output as I only had a few minutes. ~~What is missing is to clear the meal input when the meal type changes - that's the last part.~~ No, I see what is happening, because of the order that html is triggered, you lose the existing meal selection each time. So you need to check whether the type as really changed. I'll find some time later to find a fix.

---

<div class="post-metadata">

**Author:** ![Pioneerj](https://avatars.discourse-cdn.com/v4/letter/p/6bbea6/32.png) [@Pioneerj](https://discourse.nodered.org/u/Pioneerj)\
**Post date:** [27 September 2023 09:57 UTC](https://discourse.nodered.org/t/dynamic-drop-down-list-issue-in-node-red-editor-for-custom-node/81474/23 "2023-09-27T09:57:33Z")

</div>

Yes.. I totally agree with you.. I will follow this practice.  
Thank you ..🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 September 2023 11:50 UTC](https://discourse.nodered.org/t/dynamic-drop-down-list-issue-in-node-red-editor-for-custom-node/81474/24 "2023-09-27T11:50:58Z")

</div>

Now I've had the chance to look at the code in more detail, I've spotted a couple of other issues that arise from the way that Node-RED creates the HTML when the panel opens. I've got a fix, but real work gets in the way 🙂 .

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 September 2023 15:51 UTC](https://discourse.nodered.org/t/dynamic-drop-down-list-issue-in-node-red-editor-for-custom-node/81474/25 "2023-09-27T15:51:18Z")

</div>

Right, so that WAS actually slightly harder than I'd thought. It needed a bit of rethinking.

The issue, as mentioned, is that because Node-RED rebuilds the panel when you open it, your dynamically created drop-down list has to be recreated each time.

```auto
oneditprepare : function() {
    const that = this
    console.log("## on edit prepare")

    // NB: Will fire when panel opens & when user changes
    $('#node-input-meal_type').on('change', function() {
        const meal = $("#node-input-meal")
        
        // We need the current DOM value of the dropdown
        const mealType = $(this).val()
        
        // NB: Current DOM value and Node-RED's stored value may be different
        console.log(`🍴## meal type changed. type: "${mealType}", meal: "${meal.val()}". Stored values - Type: "${that.meal_type}", Meal: "${that.meal}"`)

        // Clear the dynamic dropdown & add a dummy entry
        meal.empty()
        meal.append('<option value="">Select an option</option>')
        
        // Add the dynamic entries depending on the meal type
        switch (mealType) {
            case 'breakfast': {
                meal.append('<option value="tea">Tea</option>')
                meal.append('<option value="milk">Milk</option>')
                break
            }
        
            case 'lunch': {
                meal.append('<option value="ham sandwich">Ham Sandwitch</option>')
                meal.append('<option value="cheese sandwich">Cheese Sandwitch</option>')
                break
            }
        
            case 'tea': {
                meal.append('<option value="burger">Burger</option>')
                meal.append('<option value="steak">Steak</option>')
                break
            }
        
            default: {
                break
            }
        }

        // Reset the meal (since we rebuilt the menu)
        $("#node-input-meal").val(that.meal)

        // If meal value is null, that means the meal type
        // changed so the previously selected meal is no longer valid
        // and we have to change it - to the default
        if ($("#node-input-meal").val() === null) $("#node-input-meal").val('')
    })
},

```

---

<div class="post-metadata">

**Author:** ![Pioneerj](https://avatars.discourse-cdn.com/v4/letter/p/6bbea6/32.png) [@Pioneerj](https://discourse.nodered.org/u/Pioneerj)\
**Post date:** [28 September 2023 14:12 UTC](https://discourse.nodered.org/t/dynamic-drop-down-list-issue-in-node-red-editor-for-custom-node/81474/26 "2023-09-28T14:12:26Z")

</div>

Hello @ralphwetzel ,  
I tried with this approach it seems to be working fine.. thank you

Thank you @TotallyInformation and @ralphwetzel ...

---

<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:** [27 November 2023 14:12 UTC](https://discourse.nodered.org/t/dynamic-drop-down-list-issue-in-node-red-editor-for-custom-node/81474/27 "2023-11-27T14:12:54Z")

</div>

Closed on 27 November 2023 14:12 UTC

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

[Previous page](https://discourse.nodered.org/t/dynamic-drop-down-list-issue-in-node-red-editor-for-custom-node/81474.md?page=1)
