Calling Javascript from a button / link click

I have defined javascript in the head section of the site (by using Wix’s add code functionality). Let’s say it is a function HelloWorld();

I then want to call this function on a button click. Everything works, if I define something like:

$w . onReady ( function () {
$w ( “#button1” ). onClick ( ( event ) => {
console.log(“Hello!”);
});
});

But the code below does not work (i.e. HelloWorld() function does not get called):

$w . onReady ( function () {
$w ( “#button1” ). onClick ( ( event ) => {
HelloWorld();
});
});

How would you actually call my HelloWorld() from a button? Perhaps, it is not possible with Wix/Velo? Next thing I was going to try was creating a custom element, but this certainly sounds like an overkill and may not work either…

NOTE: I have logged a request with Wix support, just hoping for a quick and precise answer here.

Did you add your HelloWorld() function using Custom Code in your sites section?

As stated here , you can only add the following tags to the using this functionality (scripts will not work):

  • Link

  • Meta

  • Comment

Instead, if you want to call a function write it in either your Public, Backend or directly in your Page Code depending on your needs.

Here is an example of a calling a function within the Page Code for Home :

$w.onReady(function () {
    $w('#button1').onClick((event) => {
        helloWorld();
    });
});

function helloWorld() {
    console.log("Hello world!")
}

Read here to learn more about where to put your functions depending on different scenarios.

I cannot define a function where I want to. My function MUST BE defined in the (it is a Google Ads gtag_report_conversion(url) function, and it calls other functions).

I don’t understand what this means: “if you want to call a function write it in either your Public, Backend”, but i’ll try to move all this JS to those areas(?) and see what happens. Just a shame a simple functionality like changing raw HTML is not available in Wix.

Ok, I tried to add all code to Public and then exporting my function in button_click. Got stuck on the fact that there is no dataLayer . push in Wix…

This is very sad that no one knows… Vevo support is not answering. Do I need to use some other means, or Tag Manager, or this is just impossible in Wix? People must have solved it somehow. So far I see only unsolved cases on the internet…

Did you find the solution for this, I am stuck in the exact same point and can’t find any answers online.