views:

290

answers:

3

I have several of the same class of element on a page and am attempting to get them all to be the same height even though the content within varies slightly. I've put together a small function that yields results in both Firefox 2+ and Safari 3+ but apparently doesn't even register in IE 7. Here's what I'm using:

var tallestcopy = 0;
 $(".calloutL2Copy").each(
  function(index) {
   var tallest = $(this).height();
   if (tallest > tallestcopy) { 
    tallestcopy = tallest 
   }
  });
 $(".calloutL2Copy").css("height",tallestcopy);

Where the class ".calloutL2Copy" is applied to a div containing some text and the occasional image. I've also tried replacing the last line with:

$(".calloutL2Copy").height(tallestcopy);

Again, this works in Firefox and Safari but has no effect on the same divs in IE. I've verified that IE is getting the tallest pixel value correct, it's just not applying it to the siblings. Can anyone offer up some advice to get this thing working?

UPDATE:

Here's a sample of the code from the site where I'm trying to apply this technique. I didn't write the code (and am aware there's a serious case of div-itis), I'm only trying to fix CSS errors.

<div id="calloutL2Top">
<div class="calloutL2">
 <a href="#"><img class="calloutL2Img" alt="" src="something.jpg" width="217" height="81"></a>
 <div class="calloutL2Copy">
  <h3>Lorem Ipsum</h3>
  Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
 </div>
</div>

<div class="calloutL2">
 <a href="#"><img class="calloutL2Img" alt="" src="something.jpg" width="217" height="81"></a>
 <div class="calloutL2Copy">
  <h3>Lorem Ipsum</h3>
  Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
 </div>
</div></div>

UPDATE AGAIN:

I got the go ahead from the higher powers and I'm going to post the IP of the site directly do the pages in question so you can view firsthand what the problem is:

[url removed]

What I'm trying to fix are the series of boxes along the bottom of the main content area. Hopefully this is a little more helpful.

+1  A: 

Have you tried

$(".calloutL2Copy").children().css("height",tallestcopy);

This should apply the css style to all the first level children of .calloutL2Copy.

Also, how exactly is your HTML nested? Another thing you could try would be to firebug Firefox as the script runs and see exactly what is getting your value. This might help you understand why IE isn't working. (If you have IE8, which has developer tools, you could also look at your HTML after the script and see exactly what has gotten the css and what hasn't).

Robert DeBoer
I modified your code a bit because .calloutL2Copy is what I want to resize, not its children, and I added the pixel part from the comment beneath this. I moved the selector up to the container and again, it works in Firefox and Safari, but not in IE. Further, when I use Firebug it shows in the CSS that the heights are being applied correctly. I'm totally stumped.
Andrew
Can you use developer tools in IE8 to see what IE is doing?
Robert DeBoer
+1  A: 

I think you need to specify a unit...

$(".calloutL2Copy").css("height", tallestcopy + "px");

EDIT:

I think you are a victim of an IE bug. To fix it, I believe you'll need a clear div as the last child of your floated parent...

<div style="clear:both; font-size:1px; height:1px">&nbsp;</div>

EDIT 2:

View the source of your site. The first line needs to be the DOCTYPE, and you have a weird character there. Sup with that?!

Josh Stodola
Dough...why didn't I think of that. Probably is another good reason the script doesn't work in IE
Robert DeBoer
I have attempted to add px before like this, but to no avail.
Andrew
jq will do this if for you
redsquare
I removed the floats just to test this and I still get nothing. I have added the IP for the site in my original question for reference and perusal. Thanks for all the great ideas so far.
Andrew
I can't see a weird character there at all. As far as I see the first line (with the exception of a line break) is the doctype. There's a good deal of PHP that's executed before the page even loads, so I'm assuming the line break is a result of that.
Andrew
The DOCTYPE needs to be on the first line. Simple as that.
Josh Stodola
When I view the source (IE7, XP, opens in Notepad, font is courier new), I see a strange character on the first line. It's like a bullet ·
Josh Stodola
I do see it now, but it's strange that it only appears on the second level pages (Company, Products, etc). If you view the source of the homepage or a third level page it's completely absent. I'll investigate a little more, but I think we have a winner. Thanks a mil!
Andrew
This was a character encoding issue that was throwing the browser into quirks mode because the doctype, to IE, didn't appear to be defined. Firefox and Safari are much more lenient when it comes to this and that's why they displayed it correctly.
Andrew
When you run your page throught he W3C validator, it warns you about the encoding byte marks. Glad we got to the bottom of this!
Josh Stodola
A: 

I made a test page using HTML 4.01 Strict DOCTYPE and the exact code you provided in the question, and it works as intended in IE 6, 7, and 8 for me. Possibly there's a preset height or min/max-height in your CSS that's being inherited and causing conflicts, or another manipulation of the height in other JavaScript on your page?

http://www.lifford.org/exp/testcase/stackoverflow_IEheights.html

RwL
That narrows things down quite a bit. There are a few other scripts running on the site, but they're mainly for passing variables and things of that nature. I suppose the next step for me is to start isolating pieces of CSS and seeing what is being overridden.
Andrew