# OnClick event and linking a button not possible? (solved)

**URL:** <https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [January 18, 2018, 2:09pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724 "2018-01-18T14:09:34Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattias](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mattias/32/1094_2.png) [@mattias](https://forum.wixstudio.com/u/mattias)\
**Post date:** [January 18, 2018, 2:09pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/1 "2018-01-18T14:09:34Z")

</div>

I’ve made a drop down menu, it almost works, but when someone choose a product category from the dropdown menu, i want it to link to an product category page and then close the strip i use as the dropdown menu…

i wanted to make an OnClick event that closes the strip and linking the button to a page? - But the button does not work, when i’ve made an OnClick event… Any ideas or suggestions?

**I’ve added an image to show the dropdown menu i have made**

 ![](https://us1.discourse-cdn.com/wix/original/3X/7/3/736fe577b205dd23327d53f367eee1687f99cca3.jpeg)

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/system/32/86885_2.png) [@system](https://forum.wixstudio.com/u/system)\
**Post date:** [January 18, 2018, 2:32pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/2 "2018-01-18T14:32:55Z")

</div>

Hi Mattias,

Having both link and onClick is not a good practice, as you define two different behaviours for a button.  
As a solution, at the end of your onClick function, use [wixLocation.to](https://www.wix.com/code/reference/wix-location.html#to) .

Liran.

---

<div class="post-metadata">

**Author:** ![mattias](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mattias/32/1094_2.png) [@mattias](https://forum.wixstudio.com/u/mattias)\
**Post date:** [January 18, 2018, 2:56pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/3 "2018-01-18T14:56:45Z")

</div>

Awesome i’ll try that, thanks Liran

---

<div class="post-metadata">

**Author:** ![mattias](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mattias/32/1094_2.png) [@mattias](https://forum.wixstudio.com/u/mattias)\
**Post date:** [February 2, 2018, 10:54am UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/4 "2018-02-02T10:54:42Z")

</div>

Hi Liran,  
The [wixLocation.to](http://wixlocation.to/) is not working for me…

view my post here:

> **[\[SOLVED\] Wix.location to a internal page not working? (solved) | Velo by Wix](https://community.wix.com/velo/forum/coding-with-velo/wix-location-to-a-internal-page-not-working)**
>
> I have touble making this work.. Please help
> 
> My page name is STAALPLADER
> This is my code
> import wixLocation from "wix-location";
> 
> export function SortimentButton\_click(event, $w)...

---

<div class="post-metadata">

**Author:** ![roi-bendet](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/roi-bendet/32/1283_2.png) [@roi-bendet](https://forum.wixstudio.com/u/roi-bendet)\
**Post date:** [February 4, 2018, 4:18pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/5 "2018-02-04T16:18:07Z")

</div>

Hi mattias,  
Can you please share a link to your site so we can inspect ?  
Roi

---

<div class="post-metadata">

**Author:** ![mattias](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mattias/32/1094_2.png) [@mattias](https://forum.wixstudio.com/u/mattias)\
**Post date:** [February 23, 2018, 9:30pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/6 "2018-02-23T21:30:07Z")

</div>

Sorry for the delay, i had to finish the webiste before publishing, because its a business webiste 😃 The link is below:

> **[Stålplader til tag og væg | Høj kvalitet og lave priser hos Profil Tech](https://www.profiltech.dk)**
>
> Her får du stålplader til tag og væg til de bedste priser og i den bedste kvalitet ✓ 40 års garanti ✓ Levering med kranbil

Just hover over the menu and choose “produkter” here you will find it.

---

<div class="post-metadata">

**Author:** ![giedrius-grazevicius](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giedrius-grazevicius/32/711_2.png) [@giedrius-grazevicius](https://forum.wixstudio.com/u/giedrius-grazevicius)\
**Post date:** [February 23, 2018, 10:24pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/7 "2018-02-23T22:24:38Z")

</div>

Hi Mattias,

is this still an issue? It seems to work very well.

---

<div class="post-metadata">

**Author:** ![mattias](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mattias/32/1094_2.png) [@mattias](https://forum.wixstudio.com/u/mattias)\
**Post date:** [February 23, 2018, 10:28pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/8 "2018-02-23T22:28:03Z")

</div>

No it works great it was just because i was using the page name instead of the seo url name… Stupid mistake, but it works now 😃

---

<div class="post-metadata">

**Author:** ![bastian-nachtwald](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/bastian-nachtwald/32/2207_2.png) [@bastian-nachtwald](https://forum.wixstudio.com/u/bastian-nachtwald)\
**Post date:** [February 25, 2018, 1:46pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/9 "2018-02-25T13:46:52Z")

</div>

Hello, Im trying the same thing but a bit complicated and I can’t make that link to work. I’m trying to create a button (with an icon that is a custom image) that after clicking:

1. leads to another page of my website
2. hides itself revealing what is underneath

The problem I have so far is that whenever I apply onClick to any icon the link just doesn’t work on click even if its there. I tried to link the page via the button editor and also via onClick code and there is no way i can make the icon to lead anywhere. It works as soon as I take out the onClick thingy.

Thank you so much

 ![](https://us1.discourse-cdn.com/wix/original/3X/1/e/1e73b91498a28c2afcf7408c9636ebf63eee441e.png)

---

<div class="post-metadata">

**Author:** ![mattias](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mattias/32/1094_2.png) [@mattias](https://forum.wixstudio.com/u/mattias)\
**Post date:** [February 25, 2018, 1:54pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/10 "2018-02-25T13:54:55Z")

</div>

Try this:

import wixLocation from “wix-location”;

export function moninactive\_click(event, $w) {  
wixLocation.to(“/suenos”);  
}

If its on your site, it should work with the link 🙂

---

<div class="post-metadata">

**Author:** ![bastian-nachtwald](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/bastian-nachtwald/32/2207_2.png) [@bastian-nachtwald](https://forum.wixstudio.com/u/bastian-nachtwald)\
**Post date:** [February 25, 2018, 2:33pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/11 "2018-02-25T14:33:32Z")

</div>

Thanks a lot Mattias, that worked!!

---

<div class="post-metadata">

**Author:** ![mattias](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mattias/32/1094_2.png) [@mattias](https://forum.wixstudio.com/u/mattias)\
**Post date:** [February 25, 2018, 2:50pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/12 "2018-02-25T14:50:39Z")

</div>

You’re welcome, im glad it helped 🙂

---

<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, 5:48am UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/13 "2018-05-07T05:48:57Z")

</div>

Hi Mattias,

I’m facing a similar issue.  
I’m trying to attach a trackevent to a button click. It seems to work fine when the link on the button is to an external page e.g. Google. But when the link is to another page on my own website , the link does not work, although the event does get triggered.

I tried your suggetion of WIxLocation.to (“/page”) given above, but am getting an "Unexpected token " error message - it seems to have a problem with the period between wix and location.

Could you help, please?

Thanks,  
Naresh

---

<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, 5:51am UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/14 "2018-05-07T05:51:43Z")

</div>

BTW - What IS an Unexpected token error, and how can it be fixed?  
Sorry for the naive question - I am not a coder :0)

---

<div class="post-metadata">

**Author:** ![mattias](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mattias/32/1094_2.png) [@mattias](https://forum.wixstudio.com/u/mattias)\
**Post date:** [May 7, 2018, 6:10am UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/15 "2018-05-07T06:10:10Z")

</div>

Hi Kumarnareshk333,

i’m also kinda new to coding, so no problem 😉

Actually, i can’t se what the problem should be, can you maybe take a screen shot of your code? 🙂

An unexpected token, comes from a minor mistake forexample, you forgot the " } " in the en of the code, then there will come an unexpected token… So maybe just an missing symbol, or something, but send me a screenshot, then i can see if i can figure out where the problem is 🙂

---

<div class="post-metadata">

**Author:** ![oshrimaidanik](https://avatars.discourse-cdn.com/v4/letter/o/dbc845/32.png) [@oshrimaidanik](https://forum.wixstudio.com/u/oshrimaidanik)\
**Post date:** [June 1, 2018, 4:04pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/16 "2018-06-01T16:04:49Z")

</div>

Hi Liran and Roi,  
I agree on " define two different behaviors for a button " is not good.  
But in the case on GA event it is necessary.  
In addition it contradicts this:

> **[Velo Tutorial: Sending Tracking and Analytics Events | Help Center | Wix.com](https://support.wix.com/en/article/velo-tutorial-sending-tracking-and-analytics-events)**
>
> Visit the Velo by Wix website to onboard and continue learning.Adding tracking code to your site allows you to monitor how users interact with your site. Collec

Therefore it is a bug, and using wixLocation is a workaround to live with the bug.  
Best,

---

<div class="post-metadata">

**Author:** ![jared](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@jared](https://forum.wixstudio.com/u/jared)\
**Post date:** [February 7, 2020, 10:45pm UTC](https://forum.wixstudio.com/t/onclick-event-and-linking-a-button-not-possible-solved/19724/17 "2020-02-07T22:45:30Z")

</div>

Has this been solved… running into the same thing right now – it essentially broke my site and didn’t realize.
