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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:12:55+00:00 2026-05-20T12:12:55+00:00

When looping over a group of items using jquery selectors is there a way

  • 0

When looping over a group of items using jquery selectors is there a way to find out how many items there are on the collection?

  • 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-20T12:12:56+00:00Added an answer on May 20, 2026 at 12:12 pm

    If you’re using chained syntax:

    $(".class").each(function() {
        // ...
    });
    

    …I don’t think there’s any (reasonable) way for the code within the each function to know how many items there are. (Unreasonable ways would involve repeating the selector and using index.)

    But it’s easy enough to make the collection available to the function that you’re calling in each. Here’s one way to do that:

    var collection = $(".class");
    collection.each(function() {
        // You can access `collection.length` here.
    });
    

    As a somewhat convoluted option, you could convert your jQuery object to an array and then use the array’s forEach. The arguments that get passed to forEach‘s callback are the entry being visited (what jQuery gives you as this and as the second argument), the index of that entry, and the array you called it on:

    $(".class").get().forEach(function(entry, index, array) {
        // Here, array.length is the total number of items
    });
    

    That assumes an at least vaguely modern JavaScript engine and/or a shim for Array#forEach.

    Or for that matter, give yourself a new tool:

    // Loop through the jQuery set calling the callback:
    //    loop(callback, thisArg);
    // Callback gets called with `this` set to `thisArg` unless `thisArg`
    // is falsey, in which case `this` will be the element being visited.
    // Arguments to callback are `element`, `index`, and `set`, where
    // `element` is the element being visited, `index` is its index in the
    // set, and `set` is the jQuery set `loop` was called on.
    // Callback's return value is ignored unless it's `=== false`, in which case
    // it stops the loop.
    $.fn.loop = function(callback, thisArg) {
        var me = this;
        return this.each(function(index, element) {
            return callback.call(thisArg || element, element, index, me);
        });
    };
    

    Usage:

    $(".class").loop(function(element, index, set) {
        // Here, set.length is the length of the set
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out how many possible ways there are to combine various
I'm having some trouble looping over a HashMap to print out it's values to
Im looing for a way to have a simple on mouse over-effect. (I want
I am looking for a robust way to copy files over a Windows network
I am looking for a way to have some control over the shape of
I am looking for any examples or guides to using Linq over WCF (n-tier
I'm looking over my syllabus for my theoretical computer science class and within the
I was looking over this code to calculate math.sqrt in Java. Why did they
So, I was looking over my standard cache utility when preparing to unit test
Its possible I am just really really thick. However, looking over the SDK for

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.