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. – 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
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. – 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
Share
Improve this question
asked Jun 24, 2012 at 18:04
RanadeepRanadeep
1531 gold badge4 silver badges12 bronze badges
3 Answers
Reset to default 7The 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();
Use like this:
var n = $(this).find("h2 a.widget-title").width();
jsBin demo
$("div.title-holder").each(function(){
var m = $(this).width();
var n = $("h2 a.widget-title", this).width();
var o = m - n;
alert("title: " + m + " text: " + n + " diff: " + o);
});