Skip to content

Instantly share code, notes, and snippets.

@andreburto
Created October 17, 2020 20:24
Show Gist options
  • Save andreburto/bc27553f1e7c47b251edfe69bae27afc to your computer and use it in GitHub Desktop.
Save andreburto/bc27553f1e7c47b251edfe69bae27afc to your computer and use it in GitHub Desktop.
Shadow DOM tinkering
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Shadow</title>
<script>
function addShadow() {
const theDiv = document.getElementById("theDiv2")
const shadowRoot = theDiv.attachShadow({mode: 'open'})
const para = document.createElement("p")
para.textContent = "Hello, Again!"
shadowRoot.appendChild(para)
}
function colorGreen() {
document.body.querySelectorAll("p").forEach(function (el) {
el.style.color = "#00FF00"
})
}
function shadowGreen() {
const theDiv = document.getElementById("theDiv2")
theDiv.shadowRoot.querySelectorAll("p").forEach(function (el) {
el.style.color = "#00CC00"
})
}
</script>
</head>
<body onload="addShadow()">
<div id="theDiv"><p>Hello, World!</p></div>
<div id="theDiv2">
</div>
<button onclick="colorGreen()">Green</button>
<button onclick="shadowGreen()">Shadow Green</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment