Last active
July 31, 2019 20:17
-
-
Save daniel08s/5430c462b12fe82e039ddaaacc8f0eda to your computer and use it in GitHub Desktop.
Dynamically changing content on CSS pseudo elements with custom attributes and attr() // Demo: https://jsbin.com/bezuraz
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> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
<style type="text/css"> | |
article { | |
display: block; | |
background: #ccc; | |
} | |
article::before { | |
content: 'parent: ' attr(data-parent) ' | columns: ' attr(data-columns); | |
} | |
article[data-columns='3']{ | |
width: 400px; | |
background-color: red; | |
} | |
article[data-columns='4']{ | |
width: 600px; | |
background-color: blue; | |
} | |
</style> | |
</head> | |
<body> | |
<article | |
id="electriccars" | |
data-columns="3" | |
data-indexnumber="12314" | |
data-parent="cars"> | |
</article> | |
<script> | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
var article = document.querySelector('#electriccars'), | |
data = article.dataset; | |
//alert(JSON. stringify(data)); | |
async function sleepz(ms) { await sleep(ms) }; | |
sleepz(2000).then(() => { | |
alert('finished waiting'); | |
article.dataset.parent = 'vehicles'; | |
article.dataset.columns = '4'; | |
//alert(JSON. stringify(data)); | |
}); | |
// data.columns -> "3" | |
// data.indexnumber -> "12314" | |
// data.parent -> "cars" | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment