top of page
Hello Custom Element

Hello Custom Element

Get started with Custom Elements.

Intermediate.png

Intermediate

4K

Published:

February 26, 2023

Girl Enjoying her Drink

by

Anchor 1
Hire a Developer

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.

Example Code

Tab 1

.

Tab 2

.

Tab 3

.

This code solution can be complicated.

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.

APIs We Used

Non-code example.

Related Articles

Article Link

Hire a Developer

Velo solutions are powerful tools, but building them on your own can be challenging. Let an experienced Velo development shop build it for you, so you can keep working on your site or business.

Related Examples

Did this help?

Yes

|

No

Thanks for your feedback!

Image placeholder.png

Add to Cart Gallery

Let customers to add products to their cart

Intermediate.png

INTERMEDIATE

Image placeholder.png

Hide and Show Elements

Hide and show elements in response to user interactions

Begginer.png

BEGGINER

Image placeholder.png

Hide and Show Elements

Hide and show elements in response to user interactions

Begginer.png

BEGGINER

Anchor 2
bottom of page