Example Description
Custom Elements allow you to embed a Web Component in a page. In this example, we demonstrate sending information from the page to the Custom Element and from the Custom Element to the page.
Note that custom elements only work fully on premium sites with a connected domain.
How We Built It
Page Elements
We split the HOME page into 2 sections.
The top section contains regular Wix elements. It has 2 text elements: one is used as a label and the other is used to display information passed to the page from the Custom Element. This section also contains a button used to send a message to the Custom Element.
The bottom section contains a Custom Element. Inside the element we use CSS, and JavaScript to create 2 text elements and a button. One text element is used as a label and the other is used to display information passed to the Custom Element from the page. The button used is used to send a message to the page.
Page Code
We added 2 event handlers to the HOME page.
The first event handler runs when the button to send messages from the page to the Custom Element is clicked. It uses the Wix `setAttribute()` function to send the message.
The second event handler runs when a message is received from the Custom Element. It populates the received message into the text element with the ID `message`.
Custom Element Code
The Custom Element's code can be found in the Public section of the Velo Sidebar.
The code begins with some functions that create HTML elements that will be added to the Custom Element.
Next there is a function that creates the styles for the HTML elements.
Finally, there is a class that defines the Custom Element itself. Inside the class, there is code for adding HTML elements to the Custom Element, including code for sending a message to the page when the button is clicked. There is also code for handling messages sent from the page.
Related Examples
Did this help?
|
Thanks for your feedback!
Add to Cart Gallery
Let customers to add products to their cart
INTERMEDIATE
Hide and Show Elements
Hide and show elements in response to user interactions
BEGGINER
Hide and Show Elements
Hide and show elements in response to user interactions
BEGGINER