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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:12:40+00:00 2026-05-26T21:12:40+00:00

I am looping through some elements and need to determine if an element has

  • 0

I am looping through some elements and need to determine if an element has a child(grandchild?) with the li tag, like in the information element below. The li elements will vary in id so I am not referencing them that way. I am currently looping through the li elements and if I check for children it always returns true because there are “a” tag children, I just want to check for ‘lil’ tag children.

            <ul id="navMenu">
                <li id="home"><a href="#home" rel="ajax">Home</a></li>
                <li id="information"><a href="#information" rel="ajax">Information</a>
                    <ul>
                       <li><a href="#credits" rel="ajax">Credits</a></li>
                       <li><a href="#lorem_ipsum" rel="ajax">Lorem Ipsum</a></li>
                    </ul>
                </li>
                <li id="contact"><a href="#contact" rel="ajax">Contact</a></li>
            </ul>

Here is what I have now…

        $('#test').load('../common.html #navMenu', function() {
            $.each($("#test #navMenu li"), function(i,v) {
                var theElement = $(v);
                if ($(theElement).children('li')){
                    alert('This Element has children');
                }
            });
        });

Thank you once again,
Todd

  • 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-26T21:12:40+00:00Added an answer on May 26, 2026 at 9:12 pm

    $(theElement).children('li') returns a jQuery object which always passes an if clause, even when it’s empty.

    Moreover, you want .find, since .children only returns direct children and not grandchildren.

    So:

    if ($(theElement).find('li').length > 0) {
    

    or:

    if ($(theElement).find('li').length) {
    // 0 won't pass an if clause, and all other numbers will, so you can eliminate `> 0`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do you loop through IQueryable and remove some elements I don't need. I
I am looping through some elements using jQuery's .each() and I want to delete
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
I was just looking through some information about Google's protocol buffers data interchange format.
In some special cases you will need a list of textboxes (to deal with
I am trying to loop through some elements in a form (radios, text boxes,
So I want to create variables by looping through some sort of name assigning
I am looping through some forms and I want to display some of the
in my XML I have some child elements with the same name as the
I'm looking through some code for learning purposes. I'm working through this portion of

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.