# Using functions in oneditprepare and oneditsave

**URL:** <https://discourse.nodered.org/t/using-functions-in-oneditprepare-and-oneditsave/76374>\
**Category:** Developing Nodes\
**Created:** [10 March 2023 03:31 UTC](https://discourse.nodered.org/t/using-functions-in-oneditprepare-and-oneditsave/76374 "2023-03-10T03:31:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [10 March 2023 03:31 UTC](https://discourse.nodered.org/t/using-functions-in-oneditprepare-and-oneditsave/76374/1 "2023-03-10T03:31:24Z")

</div>

I am re-factoring a couple of custom nodes and running into the limits of my understanding of JavaScript or the structure of Node-RED (or possibly both). I have large blocks of code that I use in several places in both the oneditprepare and oneditsave functions. I would like to put that code in a separate function called from both, but I have not figured out how to do it in the .html file. I have tried putting the function definition both inside and outside the RED.nodes.registerType section, but invoking it never seems to do anything. I must be missing something simple, but not so simple that I can find it by trial and error. I would appreciate any help.

---

<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:** [10 March 2023 06:51 UTC](https://discourse.nodered.org/t/using-functions-in-oneditprepare-and-oneditsave/76374/2 "2023-03-10T06:51:23Z")

</div>

This should do what you intend to do:

```javascript
<script>
    function whatever() {
        console.log("whatever");
    }

    RED.nodes.registerType("whatever-node", {
        oneditprepare: function() {
            whatever();
        }
    })
</script>

```

---

<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:** [10 March 2023 09:30 UTC](https://discourse.nodered.org/t/using-functions-in-oneditprepare-and-oneditsave/76374/3 "2023-03-10T09:30:16Z")

</div>

Hi Mike,

If you care to check the uibuilder code, there are some good examples of this.

This node has a fairly simple example: [node-red-contrib-uibuilder/editor.js at main · TotallyInformation/node-red-contrib-uibuilder (github.com)](https://github.com/TotallyInformation/node-red-contrib-uibuilder/blob/main/src/editor/uib-update/editor.js)

The main uibuilder node is a lot more complex and has many functions: [node-red-contrib-uibuilder/editor.js at main · TotallyInformation/node-red-contrib-uibuilder (github.com)](https://github.com/TotallyInformation/node-red-contrib-uibuilder/blob/main/src/editor/uib-update/editor.js)

NB: For convenience, I have a workflow that splits each node's html file into multiple parts. A Gulp watch process sticks them back together as I save. That's why the files shared are a bare js file.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [10 March 2023 18:36 UTC](https://discourse.nodered.org/t/using-functions-in-oneditprepare-and-oneditsave/76374/4 "2023-03-10T18:36:25Z")

</div>

> [@ralphwetzel](#):
>
> This should do what you intend

Thanks. I swear that was the first or second thing I tried. No clue why it didn't work, except possibly syntax error. Knowing it **should** work was a big help, and I'm now almost where I want to be.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [10 March 2023 18:39 UTC](https://discourse.nodered.org/t/using-functions-in-oneditprepare-and-oneditsave/76374/5 "2023-03-10T18:39:54Z")

</div>

> [@TotallyInformation](#):
>
> This node has a fairly simple example

Thanks for the link, Julian. When it was part of my job, I hated reading other people's code, even when I knew the language. Now, I've learned almost enough JS/jQuery/html/css to begin to enjoy the process. BTW, passing `this` as an argument is a nice trick than I plan to use in future.

---

<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:** [9 May 2023 18:40 UTC](https://discourse.nodered.org/t/using-functions-in-oneditprepare-and-oneditsave/76374/6 "2023-05-09T18:40:18Z")

</div>

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