# OnClick function not working on all buttons

**URL:** <https://community.hubspot.com/t/onclick-function-not-working-on-all-buttons/88756>\
**Category:** CMS Development\
**Created:** [August 31, 2023, 8:53pm UTC](https://community.hubspot.com/t/onclick-function-not-working-on-all-buttons/88756 "2023-08-31T20:53:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mrafey](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@Mrafey](https://community.hubspot.com/u/Mrafey)\
**Post date:** [August 31, 2023, 8:53pm UTC](https://community.hubspot.com/t/onclick-function-not-working-on-all-buttons/88756/1 "2023-08-31T20:53:43Z")

</div>

I have developed action cards that have a “copy text” button on them, that when clicked copy the text that is in the component.  
Here is my HubL + HTML code:

 ![Screen Shot 2023-08-31 at 4.48.19 PM.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/3/6/36475128f5df6ba14bcc76b831814e25a1f3ab10.png)

Following is my JS Code:

 ![Screen Shot 2023-08-31 at 4.49.46 PM.jpg](https://d2qpr2jzcazej7.cloudfront.net/original/3X/0/5/05c02fbae009a4cb983ead4218ce178b11fb837d.jpeg)

Whats odd is when I run this code, only one of the buttons “Copy text” works. So for instance if I have three action cards on my page, only the first action card’s “Copy Text” button works and the other two dont.  
Anyone have any idea what it could be?  
Thanks!

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [September 1, 2023, 7:47am UTC](https://community.hubspot.com/t/onclick-function-not-working-on-all-buttons/88756/2 "2023-09-01T07:47:19Z")

</div>

Hi, @Mrafey 👋 Thanks for your question. Can you add your code to your post, please? You can use the code block tool to help with formatting. Troubleshooting code from screenshots is hard to impossible for our community members to help with.

Thanks! — Jaycee

---

<div class="post-metadata">

**Author:** ![skimura](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@skimura](https://community.hubspot.com/u/skimura)\
**Post date:** [September 1, 2023, 7:57am UTC](https://community.hubspot.com/t/onclick-function-not-working-on-all-buttons/88756/3 "2023-09-01T07:57:48Z")

</div>

@Mrafey

Hi.

The method “getElementById” returns one element

even if there are multiple elements with the same ID.

How about use `class` ?

exmaple: (not tested)

```lia-code-sample
// Add click event all [copyBtn]
const copyBtns = Array.from(document.querySelectorAll('.copyBtn'));
copyBtns.forEach((copyBtn, index) => {
 copyBtn.addEventListener('click', () => {
 // Get the text corresponding to the copy button
 const actionDescription = document.querySelectorAll('.copy-desc-text')[index];
 const copyTxt = actionDescription.textContent;
 navigator.clipboard.writeText(copyTxt);
 ...
 });
});

```

Thanks.
