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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:08:27+00:00 2026-06-06T13:08:27+00:00

I have some jquery which will calculate the items in my menu and assign

  • 0

I have some jquery which will calculate the items in my menu and assign the li with a calculated width in px.

here is the code:

$(document).ready(function(){
    $('div#new-menu-lower ul li').css('width', ($('div#new-menu-lower ul').width() / $('div#new-menu-lower ul li').length));
    $(function() {
        var menuWidth = $('div#new-menu-lower ul').width();
        var listItems = $('div#new-menu-lower ul li').length;
        var itemWidth = Math.floor(menuWidth * (1/listItems)) - 40;
        $('div#new-menu-lower ul li').css('width', itemWidth);
    });
});

The problem is listItems shows up as 38 items and it seems to calculate every single li which is wrong. It should just count the first ul li’s NOT the child elements.

Is there anything i can do to stop this happening?

  • 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-06T13:08:28+00:00Added an answer on June 6, 2026 at 1:08 pm

    Change

    var listItems = $('div#new-menu-lower ul li').length;
    

    to

    var listItems = $('div#new-menu-lower > ul > li').length;
    

    You need jQery parent > child selector to achieve your job.

    From jQuery doc:

    Selects all direct child elements specified by “child” of elements
    specified by “parent”.

    As a CSS selector, the child combinator is supported by all modern web
    browsers including Safari, Firefox, Opera, Chrome, and Internet
    Explorer 7 and above, but notably not by Internet Explorer versions 6
    and below. However, in jQuery, this selector (along with all others)
    works across all supported browsers, including IE6.

    The child combinator (E > F) can be thought of as a more specific form
    of the descendant combinator (E F) in that it selects only first-level
    descendants.

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

Sidebar

Related Questions

I have some jquery code which I am trying to translate to YUI. I
I have some jQuery code, which attempts to show the first 6 divs on
I have a jquery function loading a php file which will output some data
Alright, so I have some jQuery code that will send an AJAX request to
I have some jQuery which will be put on multiple different HTML pages which
I have some tabs using jQuery UI which work just fine, but I want
I have some onchange events setup in jQuery which are used to populate a
I have some javascript which I want to convert to jQuery... I thought it
I have a page which loads and then I do some JQuery upon it.
I have a JQuery control which has some private methods. I would like to

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.