-1

Given the following DOM:

<ul>
    <li><a class="section_0">section_1 item_1</a></li>
    <li><a class="section_0">section_1 item_2</a></li>
    <li><a class="section_1">section_2 item_1</a></li>
    <li><a class="section_1">section_2 item_2</a></li>
    <li><a class="section_2">section_3 item_1</a></li>
    <li><a class="section_2">section_3 item_2</a></li>
</ul>

How do I select every last item per section, starting with the string "section_", with exception of the last section?
I don't want to change the DOM structure because it defines the submenu tree.

The goal is to draw a line below each section.

Edit: alternatively I could add a specific class to the last element per section, but I would prefer not to.

TylerH
  • 20,799
  • 66
  • 75
  • 101
html_programmer
  • 18,126
  • 18
  • 85
  • 158
  • Correction: `li:last-child > a` selects the last item per section. – paulvs May 01 '16 at 22:15
  • You MUST define the class in the header section or as an imported file before you can call the class by name. I assumed you had already done that. –  May 01 '16 at 22:20
  • there doesn't seem to be a pure CSS implementation of what you want without having to as you suggest add a class to the last item of each section. At least not until [CSS selectors level 4](https://www.w3.org/TR/selectors4/#selected-child-index) is implemented. See the comments in [this post](http://stackoverflow.com/a/7298062/5812121). – timolawl May 01 '16 at 22:29
  • @timolawi Ok, that's too bad. – html_programmer May 01 '16 at 22:33
  • How many `
  • ` there are per section? You could use `document.querySelectorAll('li:nth-child(2n):not(:last-child)')` for two items per section.
  • – Qwerty May 15 '16 at 04:22