views:

74

answers:

3

This has been driving me mad someone help coz i don't seem to see where the problem is.

<div id="parent">
   <div id="child"></div>
</div>
<script>
   $('#parent').height(300)

   $('#child').height($('#child').parent().height()-10); //EDITTED LINE
</script>

the above is the logic of what i need to do. I check the html tag of "#parent" it say style="height: 300px;" however the child has a value of -10 :( should have 290 ; I have checked in DOM, the value is right i have also tried the .css('height') but with no luck. has anyone experienced this kind of problem ?if so whats the solution :) put me out of missary pls

Sorry guys it is the "parent().height()" where the problem stands. See script again (sorry again)

A: 

Why not just set it to 290?

$('#child').height(290);
J-P
this is not answer: because the child has a resize in it and other things that will change it's height value.Plus some other values that are taken away that are not always the same
Val
A: 

Try to create a variable befor

var childheight = $('#parent').height()-10;
$('#child').height(childheight);
chahedous
I have tried this already.. if i do for width gives me the right answer but not for height.
Val
A: 

.height() will fetch it with the 'px' attached sometimes. You must remove the 'px', as otherwise you are doing '300px' - 10. You can do this by:

var height = parseInt($('#el').height(),10);

to convert it to a number.

As such, your code should be changed from:

$('#child').height($('#child').parent().height()-10);

to:

$('#child').height(parseInt($('#child').parent().height(),10)-10);

or even better, add caching to your code to make it faster! (you don't have to refetch #child.)

var $child = $('#child');
$child.height(parseInt($child.parent().height(),10)-10);

You also forgot a semicolon on your first code line, however that my not be related.

balupton
still return value of 0;`<div id="parent" style="position: absolute; left: 0px; top: 0px; width: 1280px; height: 57px;"></div>` my screen resolution is 1280 btw so dnt be alarmed by the number:)
Val
it works with parent().width(); returns the right value. 1280 but height return 0 not 57 which is the expected value
Val
@balupton: End-of-line semicolons are optional in JavaScript.
casablanca
There could be a CSS conflict or something else. You will need to provide a demo it looks like of the problem.
balupton
i think it has something to do with "position: absolute;" in css which i think it ignores height value... like i said my script is 3 pages long so i can't show a demo ...
Val
@casablanca - Just because someone was drunk off their ass when they wrote the spec doesn't mean you should omit them, having semi-colons = no issues. Not having them = extra hours of debugging down the road.
Nick Craver
@Nick: I never suggested omitting semicolons. In fact, I always use them. I was just pointing out that that isn't related to the problem in any way.
casablanca
Did you end up sorting this out? What was the problem?
balupton