With Wix you can easily extend your site’s functionality using code. In this tutorial, we walk you through the essential steps to get you quickly up and running with code.
You’ll learn how to:
We added a text element and button to our site, and then used Wix APIs to change the element’s text when the button is clicked. To get started, watch the video tutorial, or follow the steps below.
Sign up for a Wix Studio account and log in.
To start coding in your site, you need to enable Wix developer mode.
In the left sidebar, click and then Start Coding.
This sets up your developer environment which includes a built-in code editor (IDE) at the bottom of your page, and a Code sidebar with advanced developer functionalities such as access to your site’s page code, backend and public code files, databases, code packages, and other tools and features to extend your development experience.
In this step, we add elements to the Home page of our site, and change the default text of the elements using the UI.
To do this:
In the left sidebar, click Add Elements .
Add the following elements to your site:
Use the UI to change the default text of the elements to whatever you like. We changed the text of our button to 'Click Me', and our text element to 'Hello World!'. Later in the tutorial we show how to change the text of our text element using code.
To select these UI elements in your code, do the following:
Click the button element. You can now see the button element’s ID in the Properties & Events panel next to the built-in code editor.
Change the button’s ID. We call ours 'btnClick'. It’s best practice to change all of your element IDs to more descriptive text so that you can easily identify them when you start writing code. We also changed our text element ID to 'txtMessage'.
To use the elements in our code, we need to look at the Wix API Reference. The reference contains various APIs to interact with site elements, your site’s database content, Wix business solutions, and more. In our example, we use the $w selector function to select elements on a page by ID or by type.
We select our elements by ID as follows:
Button: $w('#btnClick')
Text: $w('#txtMessage')
Note: To access and modify elements on your site, you must use the $w selector function. You can't use Web APIs or 3rd party libraries such as jQuery to access or manipulate the DOM.
Now that you know how to select UI elements in your code, let’s add an onClick()
event handler to the button. Events run when they are triggered by a specific action on their element. In our example, every time the button is clicked, the event’s code will run.
To add the event handler:
Go to the code editor at the bottom of your page. Notice the onReady()
function in your page code. We write the code for our event handler inside this function since it runs when all the page elements have finished loading.
In the onReady()
function, add the button element using the $w function and the element ID we gave it earlier, $w('#btnClick'). Notice that when you start typing '$w', you can see a list of all the available elements. This is useful to be able to find your target elements, especially as your site grows.
Attach the onClick()
event handler to the selected button element.
Now that we have a button with an onClick()
event handler, we need to define the code we want to run when the button is clicked. In our example, we want to change the text of the text element when the button is clicked.
To define the code we want to run when the button is clicked, do the following:
First add a console log in the onClick()
event handler function to test that it is working.
Now let’s run the code in Preview mode to test if it is working. Click Run on the top right of the code editor. Once in Preview mode, click your button. If everything is working as expected, you’ll see our console log, 'clicked!' printed in the Developer Console at the bottom of the page.
Now that our button’s event handler is working, we define our event. In our case, we want the text element’s text to change from the current 'Hello World!' to 'Hello from Wix Studio!' when the button is clicked.
To do this, we select our text element, $w('#txtMessage'), and then add the Text API’s .text
property:
Before publishing our site, let’s see a preview of our code in action.
Your code is up and running, and you’ve now learned the basics of how to code with Wix.
Learn more about coding with Wix: