Add an Event Handler

You can add dynamic event handlers to elements on a site to respond to visitor interactions like clicks, mouse movements, or form submissions.

To add an event handler to an element, do the following either inside or outside of the page's onReady() handler:

  1. Use the $w API to select the element you want to add an event handler to.
  2. Use the appropriate event method, such as onClick(), onMouseIn(), or onChange(), to define the event handler.
  3. Write the code you want to execute when the event occurs inside the event handler method body.

For example, the following code adds a click event handler to a button that changes the text of a text element when clicked:

Copy

Note: You can also add event handlers using these alternative methods:

  • Use the Properties & Events panel in the Wix Editor to generate the event handler registration code that you add the implementation to.
  • Use the AI Code Assistant in Wix Studio to generate custom event handler code.

See also

Did this help?