# Custom node with HTML logic ... jQuery? Location of logic?

**URL:** <https://discourse.nodered.org/t/custom-node-with-html-logic-jquery-location-of-logic/17237>\
**Category:** General\
**Created:** [28 October 2019 16:27 UTC](https://discourse.nodered.org/t/custom-node-with-html-logic-jquery-location-of-logic/17237 "2019-10-28T16:27:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kolban](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kolban/32/10366_2.png) [@kolban](https://discourse.nodered.org/u/kolban)\
**Post date:** [28 October 2019 16:27 UTC](https://discourse.nodered.org/t/custom-node-with-html-logic-jquery-location-of-logic/17237/1 "2019-10-28T16:27:10Z")

</div>

I'm working on a custom node that can have many core _modes_. When a user configures the properties of the node, I am imagining a radio button area to select between the modes:

- mode 1
- mode 2
- mode 3

Depending on which mode is selected, I wish to present the user with configuration options specific for that mode.

Now the questions:

- Where should I place my JavaScript to handle the visibility logic? For example when mode1 is selected, hide/show various fields. Should it be in the HTML file or can I place the JavaScript in a separate JS source file?
- Are there any conventions for referencing elements in the configuration panel? For example, if I have a text input field called "name", I want to make sure I reference _my_ name field and not another field somewhere on the page also called "name".
- Are there any available client (browser) side libraries that I can assume exist to help me manipulate the configuration panel? For example, jQuery?

---

<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:** [28 October 2019 16:41 UTC](https://discourse.nodered.org/t/custom-node-with-html-logic-jquery-location-of-logic/17237/2 "2019-10-28T16:41:01Z")

</div>

> [@kolban](#):
>
> Where should I place my JavaScript to handle the visibility logic

It must be in the onEditPrepare function in the html file. You can use functions of course to make things easier to follow. Check out the code for the uibuilder node for examples.

I expect that you _could_ call other js files - most simply by delivering them directly from your main js file as you would with an API - again uibuilder may give you some clues.

Personally, I develop my html file in 4 separate files, one for each section (code, ui and help) and a master template. Then I have a command line tool that merges them. You've probably guessed by now that uibuilder has that code 🙂

> [@kolban](#):
>
> Are there any conventions for referencing elements in the configuration panel?

There is a convention for input fields but not really for anything else as far as I am aware.

> [@kolban](#):
>
> Are there any available client (browser) side libraries that I can assume exist to help me manipulate the configuration panel?

Yes, both jQuery and jQuery UI are available to you. D3.js is also available of course (it is used for the graphical elements of the Editor) but probably not as helpful.

A few other libraries are available but are not documented so couldn't be garenteed to stay. You can see them in the Sources tab of your developer tools.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/068c07e2bb19e3f1edee6c172ab7d3c45f18a0f8.png)

Actually, I notice from that an external js reference in node-red-contrib-later that I'd forgotten about! Let me dig out a reference.

---

<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:** [28 October 2019 16:47 UTC](https://discourse.nodered.org/t/custom-node-with-html-logic-jquery-location-of-logic/17237/3 "2019-10-28T16:47:13Z")

</div>

To add to Julian's reply, the docs are worth checking: [Node properties : Node-RED](https://nodered.org/docs/creating-nodes/properties#custom-edit-behaviour)

`oneditprepare` (_not_ `onEditPrepare`) is the key place to put your logic.

> [@kolban](#):
>
> Are there any conventions for referencing elements in the configuration panel?

You should pick names that are not like to cause clashes. For example, giving an input an id of `name` or `input` is a bad idea. It is a good idea to prefix your ids with the something unique for your node, such as its type.

Many of the core nodes do this sort of thing. The best thing would be to find a node that does something similar to what you want, and then check out how it does it - they are all to be found here: [node-red/packages/node\_modules/@node-red/nodes at master · node-red/node-red · GitHub](https://github.com/node-red/node-red/tree/master/packages/node_modules/%40node-red/nodes)

---

<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:** [28 October 2019 16:47 UTC](https://discourse.nodered.org/t/custom-node-with-html-logic-jquery-location-of-logic/17237/4 "2019-10-28T16:47:19Z")

</div>

Well, that was easy 🕶

> <https://github.com/Lighthouse-Automation/node-red-contrib-later/blob/master/later/later.html#L27>

That is, in fact, delivered the way I suggested:

> <https://github.com/Lighthouse-Automation/node-red-contrib-later/blob/master/later/later.js#L137-L158>

Note one slight gotya on line 141 of the js fle though. `require.resolve` only works if the package defines a `main` entry in package.json.

But in your case, you can simply reference a local file. Alternatively, you can "mount" a folder containing static resources - again uibuilder has some examples - just remember to unmount them in the `done` part of your node's js file otherwise redeployment of the node results in multiple references and a memory leak.

---

<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:** [28 October 2019 16:58 UTC](https://discourse.nodered.org/t/custom-node-with-html-logic-jquery-location-of-logic/17237/5 "2019-10-28T16:58:24Z")

</div>

> [@knolleary](#):
>
> `oneditprepare` ( _not_ `onEditPrepare` ) is the key place to put your logic.

Oops! Sorry about that.
