# Callback function argument

**URL:** <https://discourse.processing.org/t/callback-function-argument/37553>\
**Category:** Beginners\
**Created:** [June 15, 2022, 3:25am UTC](https://discourse.processing.org/t/callback-function-argument/37553 "2022-06-15T03:25:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidhurwich](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/davidhurwich/32/9458_2.png) [@davidhurwich](https://discourse.processing.org/u/davidhurwich)\
**Post date:** [June 15, 2022, 3:25am UTC](https://discourse.processing.org/t/callback-function-argument/37553/1 "2022-06-15T03:25:35Z")

</div>

I am writing a program that connects P5 with the HTML DOM. I want to have a HTML button when pressed pass along an argument to a function I created. I know I can add an event and callback function to a variable. I am wondering if it possible to add an argument with the callback function.

This is what I have

HTML  
 Button

P5

let btn;  
function setup(){  
btn = select("#btn");  
btn.mousePressed(myFunction);  
}

function myFunction(){  
}

What I want to do

let btn;  
function setup(){  
btn = select("#btn");  
btn.mousePressed(myFunction(myArg));  
}  
function myFunction(arg){  
x = x + arg;  
}

Any assistance would be appreciated

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 15, 2022, 5:08am UTC](https://discourse.processing.org/t/callback-function-argument/37553/2 "2022-06-15T05:08:14Z")

</div>

> [@davidhurwich](#):
>
> `btn.mousePressed(myFunction(myArg));`

If _myArg_ is determined when you call **mousePressed()** you can use **bind()**:  
`btn.mousePressed(myFunction.bind(null, myArg));`

> **[Function.prototype.bind() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)**
>
> The bind() method of Function instances creates a new function that, when called, calls this function with its this keyword set to the provided value, and a given sequence of arguments preceding any provided when the new function is called.

Otherwise you should declare _myArg_ as a global variable.
