JS on-click on element class

Disclaimer: I’m not a JavaScript expert.

Hi,
I’m trying to set a href redirect on an element in the WiX Get Subscribers add-on.
The functionality that I want is to send the user to a different page when they hit the subscribe button but there seems to be some issue(s).

Here’s my code:
$w.onReady(function () {
//TODO: write your page related code here…
var book_btn = document.getElementByClassName(“subscribe-button”);
book_btn.onclick = function () {
window.location.href = “http://www.google.com”;
};
});

The warnings I get in the text editor:

  • ‘document’ is undefined.
  • ‘window’ is undefined.

Any help/tips are highly appreciated!

Hey Oscar,

Wix Get Subscribers addon is not part of Wix Editor itself but Application from App Market aka TPA, so it’s not really possible to alter its elements behavior using WixCode, only settings panel. In case you want to keep using Wix Get Subscribers addon it’s not possible at the moment.

Although the behavior you want can be implemented using custom form, but it will lack WixContacts integration which is not yet available for WixCode.

Also in WixCode instead of window and document objects we use our own modules and main of them is $w . For example if you want to find some element like subscribe button this is how you can use $w

$w.onReady(function () {
    // will return element with id #subscribeButton
    // that is $w.Button if you used button component for it
    // https://www.wix.com/code/reference/$w.Button.html
    const subscribeButton = $w('#subscribeButton');
    // You can also search for all components of a type
    // The next code will return all components of a type Button
    const allButtons = $w('Button');
    // And you can write multiple selectors by separating them with comma
    const elements = $w('Button, Image, #myCustomElement')
})

And as for going to some different url it can be done fairly simple via wix-location module

import wixLocation from 'wix-location'; 
// ...
$w.onReady(function () {
    $w('#subscribeButton').onClick(function () {
        wixLocation.to("http://wix.com");
    })
}) 

Hi Maksim,

Thanks a ton for your help! I’ll try this right away and get back to you if it succeeds/fails :slight_smile:

Cheers!

Hello community, I need your help, how can I save the current url in a subscription form with database, I need the code since it does not compile me, help please

Hi Alfredo,
Please review this post:

Roi