<style id="COLOR" onload="this.disabled=true">
div:last-child { /* change to last-of-type and it will always fail */
color:gold;
}
</style>
<style>
div:last-of-type { background:green }
</style>
<section id="LIST"></section>
<hr>
<button onclick="LIST.innerHTML=''">CLEAR</button>
<button onclick="COLOR.disabled=!COLOR.disabled">Toggle COLOR</button>
<hr>
Objective: Only color the LAST ADDED DIV GREEN
<ol>
<li>Click the ADD button, on Chromium all previous lines remain GREEN (incorrect)</li>
<li>Toggle the GOLD color ON, click the ADD button again, and only the last ADDED line is GREEN (correct)</li>
<li>Take out the ':active' in the shadowRoot STYLE, and only the last line is colored GREEN (correct)</li>
<li>Everything is fine on touch devices</li>
</ol>
<script>
LIST.attachShadow({mode: 'open'})
.innerHTML = `<style>:host(:active) button{background:green}</style><slot></slot><button>ADD</button>`;
LIST.onclick = () => {
//setTimeout(()=>{ // adding setTimeout solves the issue
LIST.appendChild(document.createElement('div')).innerHTML="DIV";
//})
}
</script>
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.