views:

46

answers:

3

This script takes the height value of each span element and applies it to its parent li element ( I wrote it to solve some float / layout issues). It works perfectly in Firefox and Chrome: after execution, I check the html and everything has worked alright.

However, in IE 7 and 8 (havent bothered trying with IE6, screw that) it doesnt work properly. Instead, it sets all li's height to auto.

This is my code:

$(".fase ol > li").each(function(index) {
var li_content_height = $('span', this).css('height');
$(this).css('height', li_content_height )   
});

And the HTML:

<div class="fase">
<ol>
<li>
<span>blablablablabla</span>
</li>
<li>
<span>blablablablabla</span>
</li>
</ol>
</div>
+2  A: 

Huh? Where are you defining an explicit height? Did you mean to do $('span', this).height()?

Would be useful if you provided a demo of the real code so we understand the context in regards to the layout/design...

Edit: You should show us your real underlying problem and layout so we can help you solve it without using scripting.

Edit #2: Without your scripting, the 1's become 1, 2, 3, 4, 5 properly. The horizontal margin becomes a little inconsistent. You should be able to just zoom:1 on the li. I think you need to refactor your CSS instead of rely on scripting, positive it can be solved without it. Also, you have serious markup issues because some tags are not closed properly, validate it.

meder
@meder - That won't work. You must get the heights separately. See my example: http://jsfiddle.net/C3k6N/. That should of returned "20" but it returned "10". Here's a working example: http://jsfiddle.net/C3k6N/1/
GenericTypeTea
My advice was never meant for multiple elements, I meant individually. Should be trivial to add them. His example does not contain multiple span siblings, so I didn't take that into account.
meder
@Meder, if it wasn't meant for multiple elements, what's with the `$('span', this).height()` example ;)?
GenericTypeTea
Does his example contain multiple span siblings? If he had specified that in his example, I would take that into account. Otherwise it just picks up one, based on his html.
meder
@Meder - I think I'm missing something here, does `$('span', this)` have a signifant meaning? I assumed that it would just return the first element's height?
GenericTypeTea
+4  A: 

Have you tried using height instead?

var li_content_height = $('span').height() + $(this).height();
$(this).css('height', li_content_height);

If you want the height including padding or margin, then you can use outerHeight:

var heightWithPadding = $('span').outerHeight();
var heightWithPaddingAndMargin = $('span').outerHeight(true);
GenericTypeTea
I just tried using height(), and it doesnt work in IE either. Thank you, though.
0al0
What do you mean it doesn't work?
GenericTypeTea
It does not set the height of the li elements to the value of the span height, it sets it to auto.
0al0
Did you try outerHeight instead?
GenericTypeTea
Shit, sorry, but height() DOES work, but I had to use $('span', this)height().Thank you!
0al0
+1  A: 

Use .height() instead of .css('height')

balupton
doesnt seem to make a difference...
0al0