# Run a function inside a template node using msg.payload

**URL:** <https://discourse.nodered.org/t/run-a-function-inside-a-template-node-using-msg-payload/22508>\
**Category:** Dashboard\
**Created:** [2 March 2020 15:55 UTC](https://discourse.nodered.org/t/run-a-function-inside-a-template-node-using-msg-payload/22508 "2020-03-02T15:55:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![YLM-UJM](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@YLM-UJM](https://discourse.nodered.org/u/YLM-UJM)\
**Post date:** [2 March 2020 15:55 UTC](https://discourse.nodered.org/t/run-a-function-inside-a-template-node-using-msg-payload/22508/1 "2020-03-02T15:55:34Z")

</div>

I have a progress bar with html, css and javascript code which I have written inside a template node (for dashboard). I have understand that I can display some text dynamically (coming from a msg.payload) using {{msg.payload}} but I do not get how to launch a function using my msg.payload.

So in my example, I'd like to run my function move() which is actually run when someone click the html button using my msg.payload. So I will have a button node, when clicked will run the move() function of my template node...

Thank you for your help

`[{"id":"1cd50f9d.3c71","type":"tab","label":"Flow 12","disabled":false,"info":""},{"id":"6f406d43.046d94","type":"ui_template","z":"1cd50f9d.3c71","group":"448586ea.7f42a8","name":"","order":5,"width":0,"height":0,"format":"<!DOCTYPE html>\n<html>\n<style>\n#myProgress {\n width: 100%;\n background-color: #ddd;\n}\n\n#myBar {\n width: 100%;\n height: 30px;\n background-color: #4CAF50;\n}\n</style>\n<body>\n\n\n<div id=\"myProgress\">\n <div id=\"myBar\"> </div>\n</div>\n <p id=\"msg\"> {{msg.payload}} </p>\n \n <button onclick=\"move()\">Click Me</button> \n\n<br>\n\n\n<script>\nvar i = 0;\nfunction move() {\n if (i == 0) {\n i = 1;\n var elem = document.getElementById(\"myBar\");\n var message = document.getElementById(\"msg\");\n var width = 100;\n var id = setInterval(frame, 100);\n function frame() {\n if (width <= 0) {\n clearInterval(id);\n i = 0;\n } else {\n width = width - 1;\n elem.style.width = width + \"%\";\n elem.textContent = width;\n if (width == 95) {\n \tmessage.textContent = \"forcez\";\n \t{payload: 1};\n \n }\n \n }\n }\n }\n}\n</script>\n\n</body>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":1020,"y":660,"wires":[["cc88617e.6dec8"]]},{"id":"cc88617e.6dec8","type":"debug","z":"1cd50f9d.3c71","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1250,"y":640,"wires":[]},{"id":"90364668.6c00c8","type":"function","z":"1cd50f9d.3c71","name":"","func":"msg.payload = 0;\nreturn msg;","outputs":1,"noerr":0,"x":860,"y":500,"wires":[["6f406d43.046d94"]]},{"id":"2ca038e1.0fc2c8","type":"inject","z":"1cd50f9d.3c71","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":700,"y":560,"wires":[["90364668.6c00c8"]]},{"id":"448586ea.7f42a8","type":"ui_group","z":"","name":"Compte à rebours","tab":"f75bbc9f.a03b1","order":3,"disp":true,"width":"6","collapse":false},{"id":"f75bbc9f.a03b1","type":"ui_tab","z":"","name":"Préhension SELFIT","icon":"dashboard","order":4,"disabled":false,"hidden":false}]`

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [2 March 2020 20:20 UTC](https://discourse.nodered.org/t/run-a-function-inside-a-template-node-using-msg-payload/22508/2 "2020-03-02T20:20:30Z")

</div>

> [@YLM-UJM](#):
>
> I'd like to run my function move() which is actually run when someone click the html button using my msg.payload

But it is working like you described, isn't it ? Perhaps you want some other behaviour ?

---

<div class="post-metadata">

**Author:** ![YLM-UJM](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@YLM-UJM](https://discourse.nodered.org/u/YLM-UJM)\
**Post date:** [3 March 2020 08:18 UTC](https://discourse.nodered.org/t/run-a-function-inside-a-template-node-using-msg-payload/22508/3 "2020-03-03T08:18:04Z")

</div>

Yes I'd like to be able to run the code with a node button (not with the html button), so I will have a payload to run the move() function...

---

<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:** [2 April 2020 08:18 UTC](https://discourse.nodered.org/t/run-a-function-inside-a-template-node-using-msg-payload/22508/4 "2020-04-02T08:18:09Z")

</div>

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