Skip to content

Instantly share code, notes, and snippets.

@rizalamar
Last active February 4, 2025 17:13
Show Gist options
  • Save rizalamar/4958e3a2fc0768a1ecd3b54e6a64ffd1 to your computer and use it in GitHub Desktop.
Save rizalamar/4958e3a2fc0768a1ecd3b54e6a64ffd1 to your computer and use it in GitHub Desktop.
Custom Element
<!DOCTYPE html>
<html lang="id">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Custom Element</title>
</head>
<body>
<my-header></my-header>
<script type="module"></script>
</body>
</html>
class MyHeader extends HTMLElement {
constructor() {
super();
this.innerHTML = `<button style="background: blue; color: white;">Klik aku!</button>`;
// pastikan event listener hanya untuk tombol!
this.querySelector("button").addEventListener("click", () => {
const headerText = document.createElement("h1");
headerText.textContent = "Ini Judul Website!";
document.body.append(headerText);
});
}
}
customElements.define("my-header", MyHeader);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment