# Any code added to button onClick breaks default behavior

**URL:** <https://forum.wixstudio.com/t/any-code-added-to-button-onclick-breaks-default-behavior/70896>\
**Category:** Ask the community\
**Tags:** code\
**Created:** [May 13, 2025, 12:22pm UTC](https://forum.wixstudio.com/t/any-code-added-to-button-onclick-breaks-default-behavior/70896 "2025-05-13T12:22:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ONLN\_Digital\_Marketi](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onln_digital_marketi/32/107887_2.png) [@ONLN\_Digital\_Marketi](https://forum.wixstudio.com/u/ONLN_Digital_Marketi)\
**Post date:** [May 13, 2025, 12:22pm UTC](https://forum.wixstudio.com/t/any-code-added-to-button-onclick-breaks-default-behavior/70896/1 "2025-05-13T12:22:36Z")

</div>

Hi, I’m running into a persistent issue with a Wix button.

Every time I add code using `$w('#myButton').onClick(() => { ... })`, the button’s default behavior stops working — even if I don’t override anything in the function. For example, even this simple handler breaks the link:

```js
$w.onReady(() => {
  $w('#whatsappButton').onClick(() => {
    console.log('clicked');
  });
});

```

The button is set (in the editor) to open an external WhatsApp link in a new tab. But once the code runs, the link no longer opens. I’ve tried using `wixWindow.openExternalLink()`, `window.open()`, and even just letting Wix handle it — but once `.onClick()` is present, the navigation is blocked.

Even worse, `window` and `dataLayer` are undefined in this context, so I can’t send events to Google Tag Manager either.

Is this expected behavior? Or is this a known bug in how buttons behave in Wix when code is attached?

Thanks!

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [May 13, 2025, 2:59pm UTC](https://forum.wixstudio.com/t/any-code-added-to-button-onclick-breaks-default-behavior/70896/2 "2025-05-13T14:59:06Z")

</div>

Hi, @ONLN_Digital_Marketi !!

It’s possible that the event assigned to the button in the editor is being overridden by the `.onClick()` handler in the code. I tested it on my end as well and saw the same result. 😇

---

<div class="post-metadata">

**Author:** ![Dan\_Suhr](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/dan_suhr/32/94074_2.png) [@Dan\_Suhr](https://forum.wixstudio.com/u/Dan_Suhr)\
**Post date:** [May 13, 2025, 9:36pm UTC](https://forum.wixstudio.com/t/any-code-added-to-button-onclick-breaks-default-behavior/70896/3 "2025-05-13T21:36:29Z")

</div>

Yes, any onclick event in the code panel will overwrite any onclick event added in the editor. That makes sense to me.

---

<div class="post-metadata">

**Author:** ![ONLN\_Digital\_Marketi](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onln_digital_marketi/32/107887_2.png) [@ONLN\_Digital\_Marketi](https://forum.wixstudio.com/u/ONLN_Digital_Marketi)\
**Post date:** [May 14, 2025, 9:23am UTC](https://forum.wixstudio.com/t/any-code-added-to-button-onclick-breaks-default-behavior/70896/4 "2025-05-14T09:23:33Z")

</div>

The only issue I’m still facing is that when I manually add code to the button (using .onClick() in Velo), the button stops working entirely — it no longer redirects or responds. So even though I understand that adding code overrides the default behavior, the custom code itself doesn’t function either.

Any idea what might be causing this?

---

<div class="post-metadata">

**Author:** ![noahlovell](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/noahlovell/32/118920_2.png) [@noahlovell](https://forum.wixstudio.com/u/noahlovell)\
**Post date:** [May 14, 2025, 10:51am UTC](https://forum.wixstudio.com/t/any-code-added-to-button-onclick-breaks-default-behavior/70896/5 "2025-05-14T10:51:25Z")

</div>

If you need the `onClick`, I’d recommend removing the link, and then using `wixLocationFronted` - [To | Velo](https://dev.wix.com/docs/velo/apis/wix-location-frontend/to) - to navigate to the page/place you want after doing any other things that need doing before 🙂
