Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

What are Custom HTML Elements? | How to use Custom HTML Elements in AEM? | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

What are Custom HTML Elements? | How to use Custom HTML Elements in AEM? by Albinsblog

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
}

Read Full Blog

What are Custom HTML Elements? | How to use Custom HTML Elements in AEM?

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
0 Replies