# Button with 2 functions

**URL:** <https://discourse.nodered.org/t/button-with-2-functions/41547>\
**Category:** Dashboard\
**Created:** [24 February 2021 12:28 UTC](https://discourse.nodered.org/t/button-with-2-functions/41547 "2021-02-24T12:28:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)\
**Post date:** [24 February 2021 12:28 UTC](https://discourse.nodered.org/t/button-with-2-functions/41547/1 "2021-02-24T12:28:54Z")

</div>

Hello,

I'm trying to use one same button on 2 different ways.  
My payload from the button is usually 0, it is for moving to my first tab with the ui\_control. So when the button is pressed the user moves to tab 0.

Now I have added a template wich returns me msg.payload:"true" and I want the same button to go now to tab 5. It means when template sends me msg.payload true, then go on press to tab 5, else go to tab 0.

Is there a way to make this with switch nodes, or do I have to write a function for it? And can someone give me a hint for the function?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [24 February 2021 12:52 UTC](https://discourse.nodered.org/t/button-with-2-functions/41547/2 "2021-02-24T12:52:11Z")

</div>

No need for function nodes. It is just one variable to define in context (I used global context but the flow context is same as good) and you then need to change the value of that variable when needed. The event of that variable changing is also sent to the change node before button which then figures out the label for button. Button itself sends out the variable form context.

```auto
[{"id":"3a4c8a92.66dd36","type":"ui_button","z":"9f4fbba4.68ceb8","name":"","group":"152dfbc2.b2a224","order":1,"width":0,"height":0,"passthru":false,"label":"{{payload}}","tooltip":"","color":"","bgcolor":"","icon":"","payload":"tabToGo","payloadType":"global","topic":"topic","topicType":"msg","x":410,"y":700,"wires":[["e9aee8c5.98eec8"]]},{"id":"3cc0e2a9.fe0d9e","type":"change","z":"9f4fbba4.68ceb8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"'Go to tab '&$globalContext(\"tabToGo\")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":190,"y":700,"wires":[["3a4c8a92.66dd36","f67a7e56.3ba28"]]},{"id":"16f79dbf.361cf2","type":"inject","z":"9f4fbba4.68ceb8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":800,"wires":[["e862682e.8e6318"]]},{"id":"e9aee8c5.98eec8","type":"debug","z":"9f4fbba4.68ceb8","name":"BTN OUTPUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":700,"wires":[]},{"id":"e862682e.8e6318","type":"change","z":"9f4fbba4.68ceb8","name":"","rules":[{"t":"set","p":"tabToGo","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":820,"wires":[["3cc0e2a9.fe0d9e"]]},{"id":"e758fc6.8f758","type":"inject","z":"9f4fbba4.68ceb8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":170,"y":840,"wires":[["e862682e.8e6318"]]},{"id":"f67a7e56.3ba28","type":"debug","z":"9f4fbba4.68ceb8","name":"BTN INPUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":430,"y":640,"wires":[]},{"id":"b28a92f7.4ea9c","type":"comment","z":"9f4fbba4.68ceb8","name":"Change button label ","info":"","x":200,"y":660,"wires":[]},{"id":"3cae34d6.cca44c","type":"comment","z":"9f4fbba4.68ceb8","name":"Change button target","info":"","x":380,"y":860,"wires":[]},{"id":"152dfbc2.b2a224","type":"ui_group","name":"IO","tab":"a0c30cd6.33919","order":3,"disp":true,"width":"7","collapse":false},{"id":"a0c30cd6.33919","type":"ui_tab","name":"Movement","icon":"dashboard","order":5,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)\
**Post date:** [24 February 2021 13:07 UTC](https://discourse.nodered.org/t/button-with-2-functions/41547/3 "2021-02-24T13:07:17Z")

</div>

very nice thank you!

I was thinking about to set the value 0 and 5 into flow context, but I didtn realise that I can use flow context as payload inside the button.

working for 2 weeks straight with node red, and still noob ☹

---

<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:** [26 March 2021 13:08 UTC](https://discourse.nodered.org/t/button-with-2-functions/41547/4 "2021-03-26T13:08:02Z")

</div>

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