2

Following the simple example shown here, <details>: The Details disclosure element [Mozilla.org], how can I click in the "details" to collapse those details?

For example, in the animated GIF below the upper portion is the "summary", which when clicked toggles the "details" (below).

In some instances those details will be quite long; I want to be able to click there, to collapse those details (rather than scrolling back up to the "summary" portion).

I am generating my HTML page via Ajax, so an Ajax/JQuery solution is likely what I'm after.

Here is the relevant section.

(function ($) {
    if (snippets_arr.length > 2) {
        fq_snippet += '<details><summary>' + snippets_arr.slice(0,2).join('<br/>');

        fq_snippet += '<br/>[toggle]</summary>' + snippets_arr.slice(2,).join('<br/>') + '</details>';
    }
    else {
        fq_snippet += snippets_arr.join('<br/>');
    }

  // Addendum: the solution involved this part of that file:
  init: function () {
      // add code here ...
        });
    });
  }
})(jQuery);

HTML  element


Update 1: solved, after chat with, code from @a.mola. Clicking on "summary" (top portion) toggles open / closed the "details" view (bottom portion).

New functionality: clicking on the "details" (when open, of course) collapses the "details" view.

This applies to all (i.e. multiple) "details/summary" elements on a page.

enter image description here


Update 2

I modified the code to remember and return to the scroll position, after you collapse the section.

var scrollPosition = null;

/* REMEMBER, SET SCROLL POSITION
  *
  * Here I implemented a scroll position ["scrollPosition = $(window).scrollTop(); ..."] addition
  * to my "<details>" code, so that when you collapse a long <details> section you return to the 
  * original <summary> position.
  *
  *   https://stackoverflow.com/questions/17441065/how-to-detect-scroll-position-of-page-using-jquery
  *   https://stackoverflow.com/questions/10836428/how-to-maintain-page-scroll-position-after-a-jquery-event-is-carried-out/10836745
  *   https://stackoverflow.com/questions/32573532/jquery-how-to-return-to-the-exact-same-scroll-position-when-going-back-to-previ
  */

$(document).click(function(event) {
    /* ORIGINALLY (per StackOverflow discussion with user a.mola -- referenced above):
      *
      *   if (event.target.tagName == 'DETAILS' && event.target.hasAttribute('open')) event.target.removeAttribute('open');
      *
      * Modified to following, to allow use of "scrollPosition" to reset scroll position
      * when collapsing long <details> sections.
      */

    // if (event.target.tagName == 'DETAILS' && event.target.hasAttribute('closed')) {
    if (event.target.tagName == 'SUMMARY') {
        // SAVE THE CURRENT SCROLL POSITION:
        scrollPosition = $(window).scrollTop();
        // console.log('[summary click] scrollPosition', scrollPosition, '| type:', typeof scrollPosition)
        // ----------------------------------------
        /* NOTE: "return" here exits this AND following if statement: */
        // return scrollPosition;
    }   /* if (event.target.tagName == 'SUMMARY') {} */

    if (event.target.tagName == 'DETAILS' && event.target.hasAttribute('open')) {
        scrollPosition = scrollPosition;
        // ----------------------------------------
        /* COLLAPSE <details> ELEMENT BY CLICKING ON THAT [<details>] ELEMENT:
          *   Per user a.mola:
          *   https://stackoverflow.com/questions/66609261/click-on-details-not-summary-to-close-disclosure-statement/66609543#66609543
          */
        event.target.removeAttribute('open');
        // ----------------------------------------
        // SET THE SCROLL POSITION FROM STORED SESSION VALUE:
        $(window).scrollTop(scrollPosition);
    }  /* if (event.target.tagName == 'DETAILS' && event.target.hasAttribute('open')) {} */
});    /* $(document).click(function(event) {} */
Victoria Stuart
  • 4,610
  • 2
  • 44
  • 37

1 Answers1

3

Whenever the <summary></summary> tag is clicked / touched the open attribute is set on the <details></details> tag to expose the extra content.

By simply toggling the attribute on the <details></details> tag would implement the functionality you need.

(function($) {
    // ...
    $(document).click(function(event) {
        if (event.target.tagName == 'DETAILS' && event.target.hasAttribute('open')) event.target.removeAttribute('open');
    });
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<details>
  <summary>Details</summary>
  Something small enough to escape casual notice.
</details>
a.mola
  • 3,883
  • 7
  • 23
  • Hi; thank you for the response. I saw several similar solutions (StackOverflow ...), but I could not get them to work. I thought it was because I am generating that decisions/summary in an Ajax-generated HTML page; however, even when I made a simple HTML page (I'll append that code to my question, above) using your code, it did not work in either Firefox 86 or Opera 74. This JSFiddle DOES work (with your code), however. https://jsfiddle.net/10t26hmq/ ... it is exactly what I want. – Victoria Stuart Mar 13 '21 at 03:29
  • Well then, is it all okay now? If yes please can you mark the answer correct? – a.mola Mar 13 '21 at 03:55
  • 1
    No, not yet; sorry for the confusion - see my comments: the JSFiddle and the simple HTML example work; my Ajax code is not working. – Victoria Stuart Mar 13 '21 at 04:05
  • The code you're using to generate this page from, do you know how to get exactly what you in the details tag? Can I see the code generating the HTML? – a.mola Mar 13 '21 at 04:11
  • @VictoriaStuart Yes ma, I've edited my code now... Unless you want to do something else to the `summary` tag there's no need to select it – a.mola Mar 13 '21 at 05:05
  • Hey, that's awesome, @a.mola! Very close. The issue is I have multiple details/summary elements, so that code is getting confused. I'll append another animated GIF to illustrate. :-D – Victoria Stuart Mar 13 '21 at 05:07
  • I've fixed that issue as well, this time when the `document` is clicked it checks if the element it clicked has the `open` attribute and if its tag is the `details` element. Try the code now ma @VictoriaStuart – a.mola Mar 13 '21 at 05:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229847/discussion-between-victoria-stuart-and-a-mola). – Victoria Stuart Mar 13 '21 at 05:28
  • There is a bug and code snippet gives an error. On modern FireFox at least. – WinEunuuchs2Unix Dec 25 '21 at 20:48
  • @WinEunuuchs2Unix. When I wrote the code, I just started using SO and I just used the snippet to format my code. So the `snippets_arr` variable was undefined here. But worked for her when she needed it. I've fixed it now. You can test it out – a.mola Dec 25 '21 at 21:03
  • @a.mola Works fine now. Up-voted. I'm struggling with a similar problem that you might be able to fix? [html
    when expanded requires scrolling](https://stackoverflow.com/questions/70482194/html-details-when-summary-expanded-requires-scrolling)
    – WinEunuuchs2Unix Dec 25 '21 at 21:10