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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:39:37+00:00 2026-06-13T17:39:37+00:00

You can see the problem on http://jsfiddle.net/6gnAF/1/ I’ve little problem with my list elements.

  • 0

You can see the problem on http://jsfiddle.net/6gnAF/1/

I’ve little problem with my list elements.

I’m using very simple unordered list like;

<div>
  <ul>
    <li class='button'>Element 1</li>
    ...
    <li class='button'>Element N</li>
  </ul>
</div>

(Number of list elements, N, varies on every page.)

The div which contains the ul is 696 px width. List elements, li, are horizontally aligned, and when they reach end of div’s width, they contiues on new line.

What I wanted to adding class .left the first li of every line, and class .right to the last one.

    $('li').each(function() {
        var liWidthOut = $(this).width();
        totalWidth += liWidthOut;
        if (totalWidth >= divWidth) {
            $(this).addClass("left");
            $(this).prev().removeClass("middle").addClass("right");
            totalWidth = liWidthOut;
        }
        else {
            $(this).addClass("middle");
        }
     });

I managed that with Jquery, but I’ve a little problem.

You can see the problem on http://jsfiddle.net/6gnAF/1/

The problem is, .left and .right classes change the width of li elements which are added to, so some times they create new lines, and last li elements become first one which have .right class, instead of .left.

How can I fix this?

(Sorry for the grammatically errors)

  • 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-13T17:39:39+00:00Added an answer on June 13, 2026 at 5:39 pm

    Your code is mostly right, just two nitpicks which made it go wrong:

    1. Use var liWidthOut = this.offsetWidth; instead of var liWidthOut =
      $(this).width();
      as the later is giving you the width of the inner
      text, instead of also adding the padding, as offsetWidth does.

    2. Apply the .button class before starting looping and calculating
      widths, as the .button style will affect the elements width, you have to
      apply it in the first place so your calculations don’t fail later.

    See working demo of your code with those two things corrected.

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

Sidebar

Related Questions

You can see the example here: http://jsfiddle.net/8EHED/8/ This is a tricky problem because I
Here's a very primitive solution I came up with: http://jsfiddle.net/pjD2n/1/ As you can see
I have the following code at http://jsfiddle.net/qPtXL/ As you can see the problem is
Here you can see the problem: http://jsfiddle.net/kAQrX/1/ I want the magnifier, search text and
You can see the issue here: http://jsfiddle.net/qQ6ws/36/ I am using shift to toggle my
I have this small testcase: http://jsfiddle.net/sV8js/ You can see that in Chrome (tested on
Take a look at this fiddle: http://jsfiddle.net/PRz4G/ You can see four white points about
Here you can see the code: http://jsfiddle.net/LQSK3/1/ I can't get display: inline; working there
I was playing around with this code: http://jsfiddle.net/VmVAq/ As you can see, on page
See the problem in action: http://jsfiddle.net/krtGd/1/ I have an embedded SVG element in a

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.