# How to set a global variable in a function node base on an svg event

**URL:** <https://discourse.nodered.org/t/how-to-set-a-global-variable-in-a-function-node-base-on-an-svg-event/92297>\
**Category:** General\
**Tags:** node-red-dashboard, function-node\
**Created:** [10 October 2024 15:19 UTC](https://discourse.nodered.org/t/how-to-set-a-global-variable-in-a-function-node-base-on-an-svg-event/92297 "2024-10-10T15:19:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Desmond-Zeh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/desmond-zeh/32/92216_2.png) [@Desmond-Zeh](https://discourse.nodered.org/u/Desmond-Zeh)\
**Post date:** [10 October 2024 15:19 UTC](https://discourse.nodered.org/t/how-to-set-a-global-variable-in-a-function-node-base-on-an-svg-event/92297/1 "2024-10-10T15:19:39Z")

</div>

Hello,  
I am trying to set a global variable in a function node base on an svg js event.  
I want that the variable should only be set when an event occur in the svg node (when the user click on a button in the svg node). Here below is my code which isn't responding as needed.

```auto
{
   "command":"add_js_event",
    "event":"click",
    "elementId":"alarm",
    "script":"global.set('fault',1)"
    }

```

Any help on this, pls....

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 October 2024 15:44 UTC](https://discourse.nodered.org/t/how-to-set-a-global-variable-in-a-function-node-base-on-an-svg-event/92297/2 "2024-10-10T15:44:49Z")

</div>

> [@Desmond-Zeh](#):
>
> `"script":"global.set('fault',1)"`

scripts running in SVG (or HTML) events are executed on the client side (AKA in the browser) not in the server (AKA Node-RED)

If you want to affect global context, feed the value back to Node-RED then use a change node or function node to set the global context.

I assume you are using node-red-contrib-ui-svg?

Read its docs - you can have events `send` values back to node-red (they come out of the SVG node) - wire that to a debug to see what you get.

---

<div class="post-metadata">

**Author:** ![Desmond-Zeh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/desmond-zeh/32/92216_2.png) [@Desmond-Zeh](https://discourse.nodered.org/u/Desmond-Zeh)\
**Post date:** [10 October 2024 16:18 UTC](https://discourse.nodered.org/t/how-to-set-a-global-variable-in-a-function-node-base-on-an-svg-event/92297/3 "2024-10-10T16:18:34Z")

</div>

You're right, it works.  
Thanks Steve

---

<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:** [24 October 2024 16:18 UTC](https://discourse.nodered.org/t/how-to-set-a-global-variable-in-a-function-node-base-on-an-svg-event/92297/4 "2024-10-24T16:18:47Z")

</div>

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