# Multiple Button one Function.

**URL:** <https://forum.wixstudio.com/t/multiple-button-one-function/36336>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [December 22, 2017, 12:29am UTC](https://forum.wixstudio.com/t/multiple-button-one-function/36336 "2017-12-22T00:29:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_geo](https://avatars.discourse-cdn.com/v4/letter/_/c0e974/32.png) [@\_geo](https://forum.wixstudio.com/u/_geo)\
**Post date:** [December 22, 2017, 12:29am UTC](https://forum.wixstudio.com/t/multiple-button-one-function/36336/1 "2017-12-22T00:29:37Z")

</div>

Good Day! I want to do the same thing when button1 is clicked. So I have to write the same function again for button2 and button3.

function validateFields(e) {  
code here;  
}

$(function() {  
$(‘#button1’).click(validateFields);  
$(‘#button2’).click(validateFields);  
$(‘#button3’).click(validateFields);  
});

---

<div class="post-metadata">

**Author:** ![mikhail-spirin-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikhail-spirin-wix/32/838_2.png) [@mikhail-spirin-wix](https://forum.wixstudio.com/u/mikhail-spirin-wix)\
**Post date:** [December 22, 2017, 11:13am UTC](https://forum.wixstudio.com/t/multiple-button-one-function/36336/2 "2017-12-22T11:13:01Z")

</div>

Hi! Yes, sure it’s possible, almost in the way you describe it. You can also do this with export function, where you execute this one-for-all function

What are the troubles you experience?

---

<div class="post-metadata">

**Author:** ![kumarnareshk333](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kumarnareshk333/32/3395_2.png) [@kumarnareshk333](https://forum.wixstudio.com/u/kumarnareshk333)\
**Post date:** [May 7, 2018, 2:00pm UTC](https://forum.wixstudio.com/t/multiple-button-one-function/36336/3 "2018-05-07T14:00:43Z")

</div>

How do I execute a one for all function?

For example, I have a page with many text links, image links and button links. Whenever any of them is clicked on this page, I would like the event to be tracked (using a wixWindow trackevent bit of code to send a signal to a Facebook pixel).

It’s painful installing an event handler code for every element on this page…I am sure there must be one bit of code that does the job for ALL clicks on this page.

Would you be able to help, please?

Thanks,  
Naresh

---

<div class="post-metadata">

**Author:** ![\_tal](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/_tal/32/669_2.png) [@\_tal](https://forum.wixstudio.com/u/_tal)\
**Post date:** [May 8, 2018, 2:38pm UTC](https://forum.wixstudio.com/t/multiple-button-one-function/36336/4 "2018-05-08T14:38:24Z")

</div>

Hi Naresh,  
You can create an array of all IDs and use forEach loop to add an event listener to each event. Here’s an example to better explain what I was referring:

```javascript
$w.onReady(function () {

   let buttonsIds= ['idElement1', 'idElement2', 'idElement3'];
   buttonsIds.forEach((idElem)=>{
      $w(`#${idElem}`).onClick(()=>{
         validateFields();
      });
   });
});

```

I hope it’s clear.

Have a good day,  
Tal.

---

<div class="post-metadata">

**Author:** ![patrickwesterkamp](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/patrickwesterkamp/32/8122_2.png) [@patrickwesterkamp](https://forum.wixstudio.com/u/patrickwesterkamp)\
**Post date:** [June 15, 2019, 9:43pm UTC](https://forum.wixstudio.com/t/multiple-button-one-function/36336/5 "2019-06-15T21:43:22Z")

</div>

I couldn’t get this code to work for all the elements on the age. I may be doing the {idElem} part incorrectly?

```javascript

$w.onReady(function () { 

 let buttonsIds= ['checkbox1', 'input1', 'uploadButton1', 'checkbox2', 'input2', 'uploadButton2', 'checkbox3', 'input3', 'uploadButton3', 'checkbox4', 'input4', 'uploadButton4',
 'checkbox5', 'input5', 'uploadButton5', 'checkbox6', 'input6', 'uploadButton6', 'checkbox7', 'input7', 'uploadButton7', 'checkbox8', 'input8', 'uploadButton8', 
 'checkbox9', 'input9', 'uploadButton9', 'checkbox10', 'input10', 'uploadButton10'];
   
 buttonsIds.forEach(()=>{
      $w(`#${buttonsIds}`).onClick(()=>{
         $w("#redSave").show();
      });
   }); 

```

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [June 16, 2019, 6:27am UTC](https://forum.wixstudio.com/t/multiple-button-one-function/36336/6 "2019-06-16T06:27:27Z")

</div>

Here’s a quickie example that I tested:

```javascript
let buttonIDs = ['#button1', '#button2', '#button3'];
buttonIDs.forEach((button) => {
    $w(button).onClick((event, $w) => {
        console.log('button', event.target.id);
    })
})

```

You can also get all buttons on the page like this:

```javascript
let buttonElements = $w("Button");

```

Then loop through the buttonElements array.

You can also directly add an event handler to all of the buttons on the page at once:

```javascript
$w("Button").onClick((event, $w) => {
    console.log('button', event.target.id);
})

```

I hope this helps. Good luck.

---

<div class="post-metadata">

**Author:** ![kovandras91](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/kovandras91/32/31816_2.png) [@kovandras91](https://forum.wixstudio.com/u/kovandras91)\
**Post date:** [May 31, 2022, 2:15pm UTC](https://forum.wixstudio.com/t/multiple-button-one-function/36336/7 "2022-05-31T14:15:09Z")

</div>

If you want to add a specific set of buttons that you have added consecutively and they are named “button1”, “button2”, etc. you can use a for loop, like so:

```
**let** tableButtons = []; 
**for(let** i = 20 ; i <= 41 ; i ++){ 
    tableButtons . push($w ( "#button" + i )); 
} 
console . log ( tableButtons );

```
