{"id":2519,"date":"2022-01-27T16:26:46","date_gmt":"2022-01-27T15:26:46","guid":{"rendered":"https:\/\/digitalia.culturanuova.net\/blog\/?p=2519"},"modified":"2022-01-27T16:53:23","modified_gmt":"2022-01-27T15:53:23","slug":"open-link-inside-a-details-element","status":"publish","type":"post","link":"https:\/\/digitalia.culturanuova.net\/blog\/?p=2519","title":{"rendered":"open link inside a &#8216;details&#8217; element"},"content":{"rendered":"\n<p>&#8216;Details&#8217; html tag is a very easy way to collapse\/expand a web content: you can see it in the following example. This is the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;details>\n&lt;summary>expansible title&lt;\/summary>\n&lt;p>all the content you want: text, images, video, in a nutshell: all possible content&lt;\/p>\n&lt;\/details><\/code><\/pre>\n\n\n\n<p>and this is the result:<\/p>\n\n\n\n<details>\n<summary>expansible title<\/summary>\n<p>all the content you want: text, images, video, in a nutshell: all possible content<\/p>\n<\/details>\n\n\n\n<p>But there is a problem: the collapsed content is not reachable unless you click over his &#8216;summary&#8217; (in the example above: &#8216;expansible title&#8217;) . Therefore a link <em>from outside<\/em> the details content toward some element <em>inside<\/em> that, doesn&#8217;t work, because it cannot open it.<\/p>\n\n\n\n<p>To fix this problem you can use a little javascript:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  const openDetailsIfAnchorHidden = evt =&gt; {\n  const targetDIV = document.querySelector(evt.target.getAttribute(\"href\"));\n  if ( !! targetDIV.offsetHeight || targetDIV.getClientRects().length ) return;\n  targetDIV.closest(\"details\").open = true;\n }\n\n &#91;...document.querySelectorAll(\"&#91;href^='#']\")].forEach( \n   el =&gt; el.addEventListener(\"click\", openDetailsIfAnchorHidden )\n );<\/code><\/pre>\n\n\n\n<p>to be put at the <em>bottom<\/em> of a page (otherwise it doesn&#8217;t work).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8216;Details&#8217; html tag is a very easy way to collapse\/expand a web content: you can see it in the following example. This is the code: and this is the result: expansible title all the content you want: text, images, video, in a nutshell: all possible content But there is a problem: the collapsed content is &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/digitalia.culturanuova.net\/blog\/?p=2519\" class=\"more-link\">Leggi tutto<span class=\"screen-reader-text\"> &#8220;open link inside a &#8216;details&#8217; element&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[566],"tags":[726,727,245,703],"class_list":["post-2519","post","type-post","status-publish","format-standard","hentry","category-webmaster","tag-details","tag-expand-compress","tag-javascript","tag-link"],"_links":{"self":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2519"}],"version-history":[{"count":6,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2519\/revisions"}],"predecessor-version":[{"id":2526,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2519\/revisions\/2526"}],"wp:attachment":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}