Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:26:45+00:00 2026-06-18T05:26:45+00:00

So I have a list of siblings like so; <div class=b></div> <div class=a></div> <div

  • 0

So I have a list of siblings like so;

  <div class="b"></div>
<div class="a"></div>
  <div class="b"></div>
  <div class="b"></div>
  <div class="b"></div>
  <div class="b"></div>
<div class="a"></div>
  <div class="b"></div>
  <div class="b"></div>
<div class="a"></div>
<div class="c"></div>
  <div class="b"></div>
  <div class="b"></div>
  <div class="b"></div>
  <div class="b"></div>
  <div class="b"></div>
  <div class="b"></div>
  <div class="b"></div>

(Note: The b‘s are not children of the a‘s, they’re all siblings. Indentation is for emphasis.)

In jQuery, I need to select each b, forgoing the first n elements in a streak. There is no specific type/class of element that breaks the streak, nor any dependable number of b‘s in a streak, or non-b elements in between.

If I used the selector to add a class selected, and supposing n = 2, my DOM would then look like so;

  <div class="b"></div>
<div class="a"></div>
  <div class="b"></div>
  <div class="b"></div>
  <div class="b selected"></div>
  <div class="b selected"></div>
<div class="a"></div>
  <div class="b"></div>
  <div class="b"></div>
<div class="a"></div>
<div class="c"></div>
  <div class="b"></div>
  <div class="b"></div>
  <div class="b selected"></div>
  <div class="b selected"></div>
  <div class="b selected"></div>
  <div class="b selected"></div>
  <div class="b selected"></div>

In other words, select every b except for n elements after anything else than b, or the beginning.

I’ve tried :nth-child(n+3), but that simply seems to take all the b‘s into account, despite the a‘s that break them.

I also tried fiddling around with .nextUntil() and .filter(), but it must take a better man than me to crack this. Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:26:46+00:00Added an answer on June 18, 2026 at 5:26 am

    Since these elements are all siblings that share the same parent, some clever use of sibling combinators will do the trick:

    $('.b + .b + .b').addClass('selected');
    

    For any n, simply repeat .b + n times before the last .b.

    No need for :nth-child() or any traversing/filtering functions, and is also a valid and well-supported CSS selector, in case you’re looking to apply styles to these elements.

    jsFiddle preview


    If you need to add an intermediate element before each set of .b + .b + .b elements, add the class first, then prepend the intermediate element before the first of each set of those matched elements. The first of these elements can be matched by looking behind it for an element that isn’t .selected, which excludes everything that does have a .selected directly behind it:

    $('.b + .b + .b').addClass('selected')
                     .filter(':not(.selected) + .selected')
                     .before('<div class="inserted"></div>');
    

    Updated jsFiddle preview

    If chaining too many methods gets confusing, you can always do each step separately:

    $('.b + .b + .b').addClass('selected');
    $(':not(.selected) + .selected').before('<div class="inserted"></div>');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty new to jquery/javascript, but proficient in html/css... I have a list of siblings
I have an HTML list like so: <ul> <li class=heading>Heading 1</li> <li class=heading>Heading 2</li>
i have a class like class CXmlData { String Data1=; String Data2=; List<String> lst=new
I have a source html looks like: <p class=heading-8>Emergency Care<span class=char-style-override-68>, if not already
I have list of input area in the form with id like contact1_title, contact2_title,
I'm using Jquery 1.6. I have a dropdown list that has a bunch of
I'm wondering if there's a way to list all custom taxonomies that are siblings
Kindly take a look at the sample below-- <div class=patent_bibdata> <b> First list</b> <a
I'm trying to make a dynamic list with jquery. What i have is an
I have a linked list of families. I delete one of the childs siblings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.