Created
September 25, 2022 20:56
-
-
Save pauleveritt/255162988b76e32f56d9630d3aa5bf1e to your computer and use it in GitHub Desktop.
Idiomorph with a custom element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Custom Element Morph</title> | |
<script src="https://unpkg.com/idiomorph"></script> | |
</head> | |
<body> | |
<hello-world name="World"></hello-world> | |
<button id="update">Update</button> | |
<script defer> | |
class HelloWorld extends HTMLElement { | |
constructor() { | |
super(); | |
} | |
connectedCallback() { | |
const name = this.getAttribute("name") | |
this.innerHTML = `<p>Hello <em>${name}</em></p>` | |
} | |
} | |
customElements.define("hello-world", HelloWorld); | |
const newHTML = "<div>I was <strong>updated</strong></div>"; | |
const button = document.getElementById("update"); | |
button.addEventListener("click", () => { | |
const targets = document.getElementsByTagName("hello-world"); | |
Array.from(targets).forEach((target) => { | |
Idiomorph.morph(target, newHTML, {morphStyle: 'innerHTML'}); | |
}); | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment