Created
May 26, 2010 11:51
-
-
Save joneff/414384 to your computer and use it in GitHub Desktop.
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
details, summary {display: block;} | |
details:not([open]) > :not(summary) {display: none;} |
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
<!doctype> | |
<html> | |
<head> | |
<title>Details element</title> | |
<style> | |
details, summary {display: block;} | |
details:not([open]) > :not(summary) {display: none;} | |
</style> | |
</head> | |
<body> | |
<details> | |
<summary>SUMMARY</summary> | |
<p>Other content</p> | |
<p>Other content</p> | |
<p>Other content</p> | |
</details> | |
<script> | |
if (!("open" in document.createElement("details"))) { | |
var summaries = document.querySelectorAll("details > summary"); | |
document.body.addEventListener("click", function(event) { | |
var target = event.target; | |
for (var i = 0, l = summaries.length; i < l; i++) { | |
if (target == summaries[i]) { | |
var parent = target.parentNode; | |
if (parent.hasAttribute("open")) { | |
parent.removeAttribute("open"); | |
} else { | |
parent.setAttribute("open", "open"); | |
} | |
} | |
} | |
}, false); | |
} | |
</script> | |
</body> | |
</html> |
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
if (!("open" in document.createElement("details"))) { | |
var summaries = document.querySelectorAll("details > summary"); | |
for (var i = 0; i < summaries.length; i++) { | |
var summary = summaries[i]; | |
summary.addEventListener("click", function() { | |
var parent = summary.parentNode; | |
if (parent.hasAttribute("open")) { | |
parent.removeAttribute("open"); | |
} else { | |
parent.setAttribute("open", "open"); | |
} | |
}, false); | |
} | |
} |
If I was worrying about IE6 not being able to display this snippet, I wouldn't have written in the first place.
Sure — please don’t get me wrong, I’m just pointing it out so less experienced developers don’t get bitten by the lack of browser support. If you only need to support modern browsers (and you don’t need to account for every possible edge case), this is a lovely lightweight snippet!
Also it won’t take care of the edge case where direct child text nodes are part of the
(as the CSS rule details:not([open]) > :not(summary) {display: none;} won’t target those).
Point taken
Btw, the code in the separate .js
seems to differ from the JavaScript in the HTML. Just a heads up.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And so is :not ;)
If I was worrying about IE6 not being able to display this snippet, I wouldn't have written in the first place. Sometimes (especially when possible) one should really take advantage of technology regardless the legacy players out there.