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:

JavaScript

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

Advertisement

Answer

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

JavaScript

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.

JavaScript
JavaScript

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement