OnClick evemt

Hi there I know that this is not the right place but i cant find my problem solution in the internet …
so Im trying to learn wix code annd what im trying to do is Onclick of a button an link page should open …
my code :

$w.(“button_id”).click(function(e){
e.preventDefault();
window.location = " http://www.google.com/ ";
});

but I Im having report log
“Cannot read property ‘location’ of undefined”
please help !

You need to use the correct Wix Code syntax with the appropriate Wix Code APIs.

import wixLocation from "wix-location";

$w("#button1").onClick( (event, $w) => {
    wixLocation.to("http://www.google.com/");
} );

Some points to keep in mind:

  • The Button API uses onClick() as its click event handler - not click()

  • An element ID includes a hashtag as shown in the example. “#button1” and not “button1”.

  • Wix Code doesn not allow access to the window DOM element. You can redirect using the wix-location.to() API.
    Refer to the Wix Code documentation for more information.

Good luck,

Yisrael

Thanks a lot … it works !