# Help with combine 2 functions

**URL:** <https://discourse.nodered.org/t/help-with-combine-2-functions/41532>\
**Category:** General\
**Created:** [24 February 2021 08:00 UTC](https://discourse.nodered.org/t/help-with-combine-2-functions/41532 "2021-02-24T08:00:17Z")\
**Posts on this page:** 5\
**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 08:00 UTC](https://discourse.nodered.org/t/help-with-combine-2-functions/41532/1 "2021-02-24T08:00:17Z")

</div>

Hello,

I would like to combine two functions, but I have difficulties with it.

I have the two functions:

```auto
  <script>
    function myFunction(x) {
     if (x.matches) { // If media query matches
       window.location.replace("http://somewhere.com");
    }else{
       window.location.replace("http://somewhere.com")
    }
    }
    
    var x = window.matchMedia("(max-width: 700px)")
    myFunction(x) // Call listener function at run time
    x.addListener(myFunction) // Attach listener function on state changes
  </script>

```

and:

```auto
<script>
var value = "Hello World";
// or overwrite value in your callback function ...
this.scope.action = function() { return value; }
</script>
<md-button ng-click="send({payload:action()})">
  Click me to send a hello world
</md-button>

```

So how I can send Hello World from my dashboard template when the size of the screen change??  
If I add `send({payload:action()})` to my if query, it doestn work. I just dont get how to send any msg, when something happens. The example with button is good, but its html, ho to do this with javascript inside dashboard template node?

---

<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:** [24 February 2021 09:35 UTC](https://discourse.nodered.org/t/help-with-combine-2-functions/41532/2 "2021-02-24T09:35:18Z")

</div>

untested (but should point you in the right direction)...

```auto

<script>
(function(scope) {

    function myFunction(x) {
        scope.send({payload:"hello world"});
    }
    
    var x = window.matchMedia("(max-width: 700px)")
    myFunction(x) // Call listener function at run time
    x.addListener(myFunction) // Attach listener function on state changes

    scope.$watch('msg', function(msg) {
        if (msg) {
          // Do something when msg arrives
          alert(msg.payload);
        }
    });
})(scope);
</script>

<md-button ng-click="send({payload:'hello world'})">
  Click me to send a hello world
</md-button>

```

---

<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 10:03 UTC](https://discourse.nodered.org/t/help-with-combine-2-functions/41532/3 "2021-02-24T10:03:30Z")

</div>

exactly what I was looking for. Thank you very much! you saved my day 😃

scope.send was what I was missing. I tried everything.send but nothing worked.

---

<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 10:21 UTC](https://discourse.nodered.org/t/help-with-combine-2-functions/41532/4 "2021-02-24T10:21:15Z")

</div>

I wonder why I cant use it in head section. Its need to be a widget inside a group. Do you have an idea?

---

<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:** [10 March 2021 10:21 UTC](https://discourse.nodered.org/t/help-with-combine-2-functions/41532/5 "2021-03-10T10:21:21Z")

</div>

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