JQuery expand Collapse all DT tags in the definition list (DL)

Here is an example of what I'm trying to accomplish, except that it uses lists (UL and LI): http://homework.nwsnet.de/news/ea21_turn-nested-lists-into-a-collapsible-tree-with -jquery

My data is structured using DL, DT, and DD tags, for example:

<dl>
  <dt>Root</dt>
  <dd>
    <dl>
      <dt>Coffee</dt>
      <dd>black hot drink</dd>
      <dt>Milk</dt>
      <dd>white cold drink</dd>
      <dt>Beer</dt>
      <dd>
        <dl>
          <dt>European</dt>
          <dd>Heineken</dd>
          <dt>Mexican</dt>
          <dd>Corona</dd>
        </dl>
      </dd>
    </dl>
  </dd>
</dl>

How can I use jQuery to turn each DT (and its corresponding DD content) into a resettable / extensible node, i.e. treeview?

+3
source share
2 answers

In this case, you can simply use togglewith an event handler click:

// When any dt element is clicked
$('dt').click(function(e){
    // All dt elements after this dt element until the next dt element
    // Will be hidden or shown depending on it current visibility
    $(this).nextUntil('dt').toggle();
});

// Hide all dd elements to start with
$('dd').hide();

, , toggleClass, , . .: http://www.jsfiddle.net/yijiang/EA4R5/1/

+8

Source: https://habr.com/ru/post/1781014/


All Articles