logoalt Hacker News

crooked-vtoday at 3:12 AM3 repliesview on HN

You can't actually control the open state properly from markup (the `open` attribute only sets the default state), which is why I haven't bothered with them.


Replies

bikeshavingtoday at 3:23 AM

I’m not sure this is correct. The DOM class HTMLDetailsElement has the `open` property, which you can use to read/write the details element’s state. If you’re using setAttribute/getAttribute just switch to the property.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLDetails...

show 1 reply
rhdunntoday at 7:01 AM

If you specify the same name on each `details` element they behave like an accordion automatically [1], no need for JavaScript. If you set one of them to open that one will be initially open.

[1] https://developer.mozilla.org/en-US/blog/html-details-exclus...

show 2 replies
subdavistoday at 3:21 AM

Out of curiosity, why have you needed to? This has never come up for me.

show 1 reply