I’m trying to implement a jQuery fallback for the details element. If you’ve never heard about it, it is basically a Disclosure widget. If the boolean attribute open is present, it indicates that both the summary and the additional information is to be shown to the user. If the attribute is absent, only the summary is to be shown. The following HTML and CSS achieve that.
HTML
<!-- opened -->
<details open>
<summary>Summary</summary>
<p>Additional information</p>
</details>
<!-- closed -->
<details>
<summary>Summary</summary>
<p>Additional information</p>
</details>
CSS
details summary ~ * {
display: none;
}
details[open] summary ~ * {
display: block;
}
I then added the following jQuery to add/remove the open attribute when the summary element is clicked.
jQuery
$("summary").click(function() {
if ($(this).parent().attr("open")) {
$(this).parent().removeAttr("open");
} else {
$(this).parent().attr("open", "open");
}
});
It adds and removes the open attribute, however the p element’s visibility remains unaffected in Chrome. What am I doing wrong? Here is a live example.
Updates
- It works in Firefox 4.
- manjii pointed out that
openshould be changed toopen="open"or it will not work the first time. BoltClock also provided an alternative solution. This is not the main issue though. - marcosfromero and BoltClock brought up the issue of Chrome’s dynamic styles support, which I think could be related.
It turned out to be a WebKit bug reported here.
Adding this empty rule will temporarily fix the issue I was having:
Contrary to the description in the bug report, it seems to occur when using an attribute selector followed by a descendant combinator.
However, Chrome 12 was released today and it has native support for the
detailsandsummaryelements.