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 6895161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:52:26+00:00 2026-05-27T06:52:26+00:00

http://jsfiddle.net/JamesKyle/y7hBQ/ I’m making a simple jquery counting function that adds the following classes: First

  • 0

http://jsfiddle.net/JamesKyle/y7hBQ/

I’m making a simple jquery counting function that adds the following classes:

  1. First & Last
  2. Even & Odd
  3. nth-item-# (counts in order)
  4. nth-item–# (counts backwards)

I have the first two done pretty easily, but the second two are giving me trouble since I’m new to jQuery/Javascript

You can see the jsfiddle here:
http://jsfiddle.net/JamesKyle/y7hBQ/

This is the code I’m currently using:

$(document).ready(function() {
    $('.count, ul').each(function() {

        var countChildren = $(this).children();

        $(countChildren).filter(':even').addClass('even');
        $(countChildren).filter(':odd').addClass('odd');
        $(countChildren).first().addClass('first');
        $(countChildren).last().addClass('last');

        var numberOfChildren = $(countChildren).size();

        // $(countChildren).addClass('nth-item-' + #);
        //                                        ⬆
        //                           Fill with the elements number

        // $(countChildren).addClass('nth-item--' + #);
        //                                         ⬆
        //                   Fill with the elements number (negative count)
    });
});

I’m only focusing on the commented out lines

Again here is the jsfiddle:
http://jsfiddle.net/JamesKyle/y7hBQ/

Thanks ahead of time!

UPDATE :

I have two solutions below, I plan to use this on a very large scale so if you could recommend what would be the most efficient way it would help me a lot! Thanks!!

  • 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-27T06:52:26+00:00Added an answer on May 27, 2026 at 6:52 am

    If every effiency count then you should consider reduce iterations;

    $('.count, ul').each(function() {
        var $children = $(this).children(),
            count = $children.size(),
            $item;
        $children.each(function(i) {
            $item = $(this)
                .addClass(i % 2 === 0 ? 'even' : 'odd')
                .addClass('nth-item-' + (i + 1))
                .addClass('nth-item--' + (count - i));
            if (i === 0) {
                $item.addClass('first');
            }
            if (i == count - 1) {
                $item.addClass('last');
            }
        });
    });
    

    This way you iterate on children only one time. When you use filters, it iterates and filters items according to your filter. jsFiddle here.

    A little benchmark is here.

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

Sidebar

Related Questions

http://jsfiddle.net/JamesKyle/re73T/ Trying to make a class switcher that will do the following: On Load
In the following link: http://jsfiddle.net/gCvbT/ , if you click on General and then click
Even though the following works: http://jsfiddle.net/N7D5r/ My attempt at using the same code does
I have the following code: http://jsfiddle.net/SPWWx/ I'm completely new to javascript, this is my
I need help concerning following code: http://jsfiddle.net/8PsdE/ <SELECT ID=pizzasize NAME=pizzasize> <OPTION VALUE=s>small <OPTION VALUE=m>medium
Trying to make simple jQuery function to create a scrollToTop button that fades in
http://jsfiddle.net/motocomdigital/c8Mey/1/ I'm using a twitter script which i'm binding a function to get the
http://jsfiddle.net/cD4Gr/4/ Le Code: $j(document).bind('click', function() { $j(#test).css({ bottom:0px }) }); Looking in the inspector,
http://jsfiddle.net/yxhzU/1042/ I'm trying to modify this example so that within the top carousel you
http://jsfiddle.net/yTkTd/ Another function with a class works perfectly, but this one seems to be

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.