Skip to content

Instantly share code, notes, and snippets.

@chellimiller
Created July 19, 2018 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chellimiller/138eb43a0a5b3cdedb1fa0245fea04b6 to your computer and use it in GitHub Desktop.
Save chellimiller/138eb43a0a5b3cdedb1fa0245fea04b6 to your computer and use it in GitHub Desktop.
Playing around with the HTML5 Template tag
<!doctype html>
<html>
<title>PAGE</title>
<meta charset="utf-8">
<body onload="inform()">
<template id="chelli-link">
<style>
a {
color: red;
text-decoration: none;
font-size: 4em;
}
a:hover {
color: black;
}
</style>
<a></a>
</template>
<script>
var proto = Object.create(HTMLElement.prototype, {
createdCallback: {
value: function() {
var template = document.querySelector('#chelli-link');
console.log(template);
var cloneTemplate = document.importNode(template.content, true);
var links = cloneTemplate.querySelectorAll("a");
links[0].textContent = 'helloTesting';
links[0].setAttribute('href', 'https://www.w3schools.com/jsref/met_element_setattribute.asp');
this.createShadowRoot().appendChild(cloneTemplate);
}
}
});
document.registerElement('chelli-link', {prototype: proto});
</script>
<chelli-link></chelli-link>
</body>
<script>
function inform() {
console.log('BODY RELOAD');
}
</script>
</html>
@chellimiller
Copy link
Author

document.registerElement(); is deprecated. Use customElements.define(name, constructor, options);

https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment