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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:33:53+00:00 2026-06-06T17:33:53+00:00

I am trying to calculate the width and difference between the widths of a

  • 0

I am trying to calculate the width and difference between the widths of a div and the text in a page where the same div class appears multiple times.

The HTML:

<div class="post_content">
       <div class="container">
          <div class="title-holder">
          <h2><a href="link.html" class="widget-title">Crossword Book</a></h2>
          </div>
       </div>

      <div class="container">
         <div class="title-holder">
         <h2><a href="link.html" class="widget-title">Crossword Bookstore Ltd. &ndash; Elgin Road</a></h2>
         </div>
      </div>
</div>

The CSS:

div.container{
  width: 130px;
}
div.title-holder {
  width: 130px;
  height:20px;
  text-align:center;
  background: silver;
  overflow:hidden;
  position: relative;
}
div.title-holder a {  
  position: relative;  
  white-space:nowrap;  
  left: 0px;
}
div.image{
  background: brown;
  width: 100%;
  height: 100px;
}

The following script outputs the result of the first div correctly and then repeats the same result. It is not going to the next div and giving the next result.

$("div.title-holder").each(function(){    
  var m = $(this).width();
  var n = $("div.title-holder h2 a.widget-title").width();
  var o = m - n;

  alert ("title: " + m + " text: " + n + " diff: " + o);    

}); 

The output is

First Alert: title: 130 text: 108 diff: 22
Second Alert: title: 130 text: 108 diff: 22

What I am looking to achieve is

First Alert: title: 130 text: 108 diff: 22
Second Alert: title: 130 text: 258 diff: -128
  • 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-06T17:33:55+00:00Added an answer on June 6, 2026 at 5:33 pm

    The value of:

    var n = $("div.title-holder h2 a.widget-title").width();
    

    Will always be the same (the first result of that selector query). You need to do:

    var n = $(this).find("a.widget-title").width();
    

    or more specific:

    var n = $("div.title-holder").children("h2").children("a.widget-title").width();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to calculate the difference between two times using JavaScript. It's just
I'm trying to calculate elapsed hours between two times. When calculating PM to AM
I want to calculate the difference between two dates in php. I am trying
i'm trying to use the class DateTime (php>=5.3) to calculate difference from 2 date.
I'm trying to calculate TimeSpans between dates. I have no problem with this if
I'm trying to calculate the Mean Difference average of a set of data. I
There are some situations where there are multiple methods to calculate the same value.
I am trying to calculate an interval between now and a user defined date
Trying to compare a given Time between two times to see if it's within
I'm trying to figure out how to calculate difference from previous item, when the

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.