Skip to content
Advertisement

jQuery accordion – skip a list element

I am using jQuery’s accordion on a UL where MOST of the LI items are to become accordions, each showing a list of links beneath them. Works fine. But then of course, the client wants a couple nodes in the list to be just single links in the list. They have no category and are on the top level.

Is there a way using the jQuery accordion that I can tell it to skip a node? I currently aim the accordion() method at the UL node and it turns every LI into an accordion. When I target each li individually, things render strangely.

So what I’d like is something like:

<ul class="accordion">
  <li class="leavemealone"><A>mylink</a></li>
  <li><h3>title</h3><div>list of stuff to be inside the accordion</div></li>
</ul>

Or some equivalent. I don’t see it in the doc. does such a thing exist?

Answer

The markup of your accordion container needs pairs of headers and content panels:

<div id="accordion">
  <h3>First header</h3>
  <div>First content panel</div>
  <h3>Second header</h3>
  <div>Second content panel</div>
</div>

Accordions support arbitrary markup, but each content panel must always be the next sibling after its associated header. See the header option for information on how to use custom markup structures.

If we then look at header, we see:

Selector for the header element, applied via .find() on the main accordion element. Content panels must be the sibling immediately after their associated headers.

So if we define a header, the content must be a sibling. This works great if each li is a header and content pair.

Consider the following example.

$(function() {
  $("#accordion").accordion({
    header: "li.header",
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<ul id="accordion">
  <li class="leavemealone"><a href="#">mylink</a></li>
  <li class="header">Section 1</li>
  <li>
    <p>Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum
      a velit eu ante scelerisque vulputate.</p>
  </li>
  <li class="header">Section 2</li>
  <li>
    <p>Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna.</p>
  </li>
  <li class="header">Section 3</li>
  <li>
    <p>
      Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam
      nisi, eu iaculis leo purus venenatis dui.
    </p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </li>
  <li class="header">Section 4</li>
  <li>
    <p>
      Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est.
    </p>
    <p>
      Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
    </p>
  </li>
</ul>

As you can see, each li becomes a container and it’s either a header, or it’s content. So you do not need to use h3 or div as wrappers.

Advertisement