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

  • Home
  • SEARCH
  • 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 7420345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:13:19+00:00 2026-05-29T08:13:19+00:00

I have a series of divs in two columns with some javascript dictating some

  • 0

I have a series of divs in two columns with some javascript dictating some behavior, and I’d like to have the background colors of certain div classes alternate.

Here’s the javascript that supposed to handle this:

<script type="text/javascript">
  $(document).ready(function(){
  $('#faqlist div.faq-title:nth-child(odd)').addClass('alternate');
    });
</script>

With the stylesheet calling the alternate div background color:

div.alternate { background: #c1c1c1 }

And here’s the html:

<div id="faqlist">
<div class="faq-title"></div>
<div class="faq-answer"></div>
<div class="faq-title"></div>
<div class="faq-answer"></div>
<div class="faq-title"></div>
<div class="faq-answer"></div>
</div>

Right now, the result is every div of class “faq-title” has the #c1c1c1 background color, leading me to believe that I’m missing something simple. Like the javascript suggests, I want only the alternating “faq-title” class divs to have the #c1c1c1 background color, and the “faq-answer” divs to get ignored.

jsFiddle: http://jsfiddle.net/inerdial/mRaRW/

  • 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-05-29T08:13:20+00:00Added an answer on May 29, 2026 at 8:13 am

    As you’ve discovered, nth-child takes its numbering from the parent element, not the results of the specific selector (see the jquery documentation).

    Use :even, which does what you want:

    $('#faqlist div.faq-title:even')
        .addClass('alternate');
    

    http://jsfiddle.net/uJqPg/

    Note that :even is 0-based, while nth-child (following the css spec) is 1-based, hence the even/odd mismatch. Also, :even is a jquery extension, and will not be native on any browser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a series of divs like this: <div id=available-posts> <div class=post id=unique_id> <a
I have a series of divs: <div class=some_class active>Some Content</div> <div class=some_class>Different Content</div> <div
I have a series of divs like this: <div id=listofstuff> <div id=anitem> <span class=itemname>Item1</h3></span>
I have a series of divs of the same class; some with title attributes
THE SITUATION I have a series of divs setup to contain various content: <div
I have a page with a series of divs. Each div represents a slide
I have a series of divs that are hidden and I would like to
I have a nav menu which is a series of divs, alternately a div
A quick question. I have a series of DIVs with the following classes .level1
I have a series of divs in a pattern of header/body, where a click

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.