# Can 2 buttons call the same function and detect which button was pressed?

**URL:** <https://discourse.processing.org/t/can-2-buttons-call-the-same-function-and-detect-which-button-was-pressed/19654>\
**Category:** Coding Questions\
**Created:** [April 11, 2020, 8:38am UTC](https://discourse.processing.org/t/can-2-buttons-call-the-same-function-and-detect-which-button-was-pressed/19654 "2020-04-11T08:38:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bassam](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@Bassam](https://discourse.processing.org/u/Bassam)\
**Post date:** [April 11, 2020, 8:38am UTC](https://discourse.processing.org/t/can-2-buttons-call-the-same-function-and-detect-which-button-was-pressed/19654/1 "2020-04-11T08:38:49Z")

</div>

I have 2 images on a canvas and each image has it’s own button. When the first button is pressed, the first image is selected. When the second button is pressed, the second image is selected.

```auto
  button1 = createButton("Select");
  button1.position(160, 350);
  button1.mousePressed(selectImage);
  button2 = createButton("Select");
  button2.position(560, 350);
  button2.mousePressed(selectImage);
}

function selectImage(){
  //button 1 was pressed
  //or
  //button 2 was pressed
}

```

[https://editor.p5js.org/Bassam/sketches/Sz3x2vjAg](https://editor.p5js.org/Bassam/sketches/Sz3x2vjAg)

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [April 11, 2020, 2:52pm UTC](https://discourse.processing.org/t/can-2-buttons-call-the-same-function-and-detect-which-button-was-pressed/19654/2 "2020-04-11T14:52:08Z")

</div>

You can use .bind or .call.

```auto
button1.mousePressed(_=> {
   selectImage.call(this, ‘btn-1’);
});

```

then in your `selectImage(btn)` check the btn value and call code accordingly.

---

<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:** [April 11, 2020, 4:16pm UTC](https://discourse.processing.org/t/can-2-buttons-call-the-same-function-and-detect-which-button-was-pressed/19654/3 "2020-04-11T16:16:59Z")

</div>

> Can 2 buttons call the same function and detect which button was pressed?

Yes, just check for the keyword `this` inside that callback function: 🖱

> **[this - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)**
>
> A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.

> **[switch - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch)**
>
> The switch statement evaluates an expression, matching the expression's value against a series of case clauses, and executes statements after the first case clause with a matching value, until a break statement is encountered. The default clause of a...

```javascript
function selectImage() {
  switch (this) {
    case button1:
      print('Button1');
      break;

    case button2:
      print('Button2');
      break;
  }
}

```

In normal circumstances, the keyword `this` inside a function holds the reference of the object which had invoked that function. 🤓
