![BlogImage.jpg](https://miro.medium.com/max/1050/1*UmVrgxCdng_LfCJF85Onzw.png)
Abstract
In this post, let us explore what Custom HTML Elements are and how to use Custom HTML Elements in AEM.
Custom Elements allow web developers to define new types of HTML elements.Once a custom element is defined, we can use it on par with built-in HTML elements.
Some of the important features are enabled by Custom HTML elements.
Define new HTML/DOM elements
Create elements that extend from other elements
Logically bundle together custom functionality into a single tag
Extend the API of existing DOM elements
Web Components uses Custom HTML Elements as one of the building blocks to enable reusable custom elements(Components).
RULES ON CREATING CUSTOM ELEMENTS:
The name of a custom element must contain a dash (-). So , and are valid names.
You can’t register the same tag more than once. Attempting to do so will throw a DOMException
Custom elements cannot be self-closing(), Always write a closing tag
STEPS FOR CREATING CUSTOM HTML ELEMENTS:
Define a custom element e.g.
Create a class with special methods.
class MyElement extends HTMLElement {
constructor() {
super();
// element created
}
connectedCallback() {
// browser calls this method when the element is added to the document
// (can be called many times if an element is repeatedly added/removed)
}
disconnectedCallback() {
// browser calls this method when the element is removed from the document
// (can be called many times if an element is repeatedly added/removed)
}
static get observedAttributes() {
return [/* array of attribute names to monitor for changes */];
}
attributeChangedCallback(name, oldValue, newValue) {
// called when one of attributes listed above is modified
}
adoptedCallback() {
// called when the element is moved to a new document
// (happens in document.adoptNode, very rarely used)
}
// there can be other element methods and properties
}
REGISTER THE ELEMENT:
// let the browser know that is served by our new class customElements.define("my-element", MyElement);
Refer to https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements and https://web.dev/custom-elements-v1/ for more details.
Let us quickly see how to create Custom HTML Elements to format the data in HTML.
Read Full Blog
Q&A
Please use this thread to ask the related questions.
Kautuk Sahni