I wrote the following code to give an HTML element max width/height in its parent container. I think I address the box model issues but I am wondering if anyone can see issues and or shortcomings.
Are there other solutions out there that accomplish this? I want to make sure I didn't re-invent the wheel.
function heightBuffer(control) {
return parseInt((control.outerHeight(true)) - control.height());
}
function widthBuffer(control) {
return parseInt((control.outerWidth(true)) - parseInt(control.width()));
}
function MaxHeightInParent(control, minHeight, controlHeightsToSubtract) {
var h = parseInt(control.parent().height()) - heightBuffer(control);
if (controlHeightsToSubtract != null)
$.each(controlHeightsToSubtract, function(index, value) {
h = h - parseInt(value.outerHeight(true));
});
if (minHeight != null && h < minHeight) h = minHeight;
control.height(0);
control.css('min-height', h);
}
function MaxWidthInParent(control, minWidth, controlWidthsToSubtract) {
var w = parseInt(control.parent().width()) - widthBuffer(control);
if (controlWidthsToSubtract != null)
$.each(controlWidthsToSubtract, function(index, value) {
w = w - parseInt(value.outerWidth(true));
});
if (minWidth != null && w < minWidth) w = minWidth;
control.width(0);
control.css('min-width', w);
}
Note controlHeightsToSubtract / controlWidthsToSubtract are if you wish to pass an array of controls that share the containing element with the element you are attempting to maximize in Height/Width.