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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:30:51+00:00 2026-06-08T12:30:51+00:00

Does anyone know how to count just the first level children of an element

  • 0

Does anyone know how to count just the first level children of an element from inside a .each() loop?

For example, I have several lists on my page, like so …

<ul>
    <li><a href="#">item1</a></li>
    <li><a href="#">item2</a></li>
    <li><a href="#">item3</a></li>
    <li><a href="#">item4</a></li>
</ul>
<ul>
    <li><a href="#">item1</a></li>
    <li><a href="#">item2</a></li>
    <li><a href="#">item3</a></li>
    <li><a href="#">item4</a></li>
    <li><a href="#">item5</a></li>
    <li><a href="#">item6</a></li>
</ul>
<ul>
    <li><a href="#">item1</a></li>
    <li><a href="#">item2</a></li>
    <li><a href="#">item3</a></li>
</ul>

I’m trying to loop through each list and determine how many list items it has, except using jQuery .size() is counting the tags as elements too. I just need to know how many list items exist.

Additionally, I’m not sure how to do this because using something like jQuery(‘ul > li’).size() isn’t accessible from within the .each() loop, because I’m referencing each different with jQuery(this). Is there a way to do something like jQuery(this ‘ > li’)? Cause that’s not working for me either 🙂

here’s what i’ve been trying, but I’m not having any luck:

jQuery('ul').each(function() {
    var count = jQuery(this).find('li').size();
    alert(count);
});
  • 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-08T12:30:52+00:00Added an answer on June 8, 2026 at 12:30 pm

    you can use length property:

    $('ul').each(function() {
        var count = $(this).find('li').length;
        alert(count);
    });
    

    or if you want to select immediate children li tags, you can try:

    $('ul').each(function() {
        var count = $('> li', this).length;
        alert(count);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know why in Oracle 11g when you do a Count(1) with more
Does anyone know an API or other method that I could generate a short
Does anyone know what is the maximum number of characters you can display in
Arrrgh. Does anyone know how to create a function that's the multibyte character equivalent
Does anyone know how or where I can go to quickly extract a list
Does anyone happen to know if, in .NET (4.0, if it matters), the Length
I have cheated every time I've needed to do a line count in XSLT
Does anyone stick to any rules (or are you forced to stick to any
First of all, this is not homework! :) Say I have an NSString: the?or?
I have a large dataframe of doctor visit records. Each record (row) can have

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.