top of page
Maveristic

Be Valuable, Not Available

Writer's pictureCanute Fernandes

Wix Event Handlers: Exploring the New Dynamic Approach and How to Migrate Your Code

Updated: Oct 1


Hey everyone! Recently, Wix rolled out an exciting update to their coding capabilities, making it much more dynamic and user-friendly. In this blog post, I'll walk you through the changes, explain the new dynamic event handlers, and show you how to migrate from the old static ones without losing any functionality.


What’s New with Wix Event Handlers?

Previously, Wix used static event handlers to manage user interactions on your website. While these worked fine, they were somewhat rigid and required more steps to set up. Now, Wix has introduced dynamic event handlers, making your coding experience smoother and more flexible.

The main change is that when you create a new event, it automatically adopts the dynamic format, which starts with $w. This means there's no need to export functions manually or set up event handlers in the old way. This dynamic method is more in line with best practices and simplifies coding on your Wix site.


How We Used to Do It: The Static Approach

Earlier, to add an event handler, you’d select the element (say, a button), click on the event (like "onClick"), and it would create a static event handler, starting with an export function. Here's an example of what that looked like:

export function myButton_click(event) {
	console.log("Button Clicked");
	//Your Code Here
}

While this approach worked, it was a bit clunky. You had to manually wire these handlers in the code, and updating or migrating them wasn’t as straightforward.


What’s Changed: The Dynamic Event Handlers

Now, Wix has shifted to a more dynamic experience for all ongoing and future projects. When you create an event handler today, it uses the dynamic format that begins with $w. There’s no need for the "export" function anymore. Instead, the event handler code starts directly with the element’s ID and the event type.


Example of the New Dynamic Event Handler:

$w("#myButton").onClick((event) => {
	console.log("Button Was Clicked");
	//Your Code Here
}

This dynamic approach is not only easier to work with but also allows you to add multiple handlers for the same event, giving you much more flexibility.


How to Add a Dynamic Event Handler

Adding an event handler with this new method is quite simple:

  • Select the element in the Wix Editor (for example, a button).

  • In the Properties & Events panel, click on the event you want to handle, such as "onClick."

  • The event handler code is automatically generated at the bottom of your page code:

$w("#myButton").onClick((event) => {});
  • You can now add the custom code you want to run when the event occurs. For example:

$w("#myButton").onClick((event) => {
	console.log("Button Was Clicked");
	$w("#myText").text = "Hello, World!";
}

And that’s it! The code now executes when the event happens, and the new dynamic format makes it easier to manage.


Migrating Existing Code: No Need to Worry!

If you’ve already implemented code using the old static method, don't worry! There’s no need to delete or rewrite everything. Wix has made the transition seamless with a simple migration process.


How to Migrate Your Code

  1. Go to the Respective Page: Find the element with the static event handler. In our example, this might be a line or a button where you've already added an event.

  2. Open the Code Panel: Once the element is selected, open the code panel. You'll notice a yellow warning icon next to the static event handler.

  3. Hover and Migrate: Hover over the yellow icon, and you'll see the option to "Migrate." Click on this, and it will automatically convert your code to the new dynamic format.

This process updates the event handler without removing or breaking the existing code. For instance, an old format like this:

export function myButton_click(event) {
  // Old code here
}

will be transformed into:

$w("#myButton").onClick((event) => {
  // Migrated code here
});

No need to adjust any other part of your code—it will continue to function as it did before, but now in the updated, dynamic format.


Highlight: Improved Code Panel Experience

Wix has also enhanced the coding experience with the latest update. One of my favorite new features is how the code panel now sticks the event handler at the top as you scroll. This helps identify the code you're currently working on, making it easier to manage and organize your event handlers.


Testing Your Migrated Code

After migration, you can test your pages to ensure everything works correctly. From my own experience, all migrated event handlers, including repeaters and "onChange" activities, worked seamlessly without any manual adjustments. Just migrate the event handlers, test them on your page, and they should function just like before.


Final Thoughts: Don’t Panic, Just Migrate!

If you’re working on an ongoing project or starting a new one, this update is meant to make your coding process easier and more dynamic. Remember, there's no need to panic or delete your existing events. Simply use the migration tool to update them to the new format.

This change opens up a more flexible and organized coding environment in Wix, allowing you to create more dynamic user interactions with ease.


Happy coding, everyone!


Want to Dive Deeper? For more detailed information on Wix’s dynamic event handlers, check out the official documentation here.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page